a WebSocket is a protocol that allows bidirectional communication between a client(running on a browser) and a server. It can have interesting applications such as push notifications and chat. As of HTML 5, WebSocket has been introduced as part of the standard javascript. WebSocket operation depends on the browser type and its version, but normally it is supported by all the modern browsers. In this example, we are going to wrap the WebSocket API using JsInterop, and then use it from a GWT app to send and receive messages from a Spring Boot based server.
The WebSocket wrapper
The Server
Then we can initialize a WebSocket from our application:
We have used lambdas for event handlers to take advantage of GWT 2.8.0 Java 8 capabilities. Finally, we have used Spring Boot, and Spring’s support for WebSockets to implement a server that sends a notification each 2 mins to our app, and also can receive messages and display them on the console.
Our WebSocket configuration looks like:
The SocketHandler implementation can be found here.
and finally, we need to implement our scheduling module that sends a message every two minutes: