Websockets are one of the means to push data from a backend application to a web application running in the browser. In this post, we will look at hosting a web socket server within a java/scala application using Atmosphere/Nettosphere. Atmosphere is an asynchronous application development framework that allows development of realtime applications.
Shown below is the scala code that launches a websocket server on localhost at 8080. When the application starts, Atmosphere scans the classpath to locate relevant resources that could be loaded as handlers. In this case, it finds the WebSocketChat class which has the attribute “WebSocketHandlerService”. This handler is made to handle incoming websocket requests at the path “/echo”.
For this to work, the build.sbt should be modified to include the dependencies.
Once the application is up and running, you will be able to connect to it using regular WebSocket clients. For example, shown below is a simple html file that connects to the websocket we just launched and pings to it.
I have some plans on using this to build some webparts based system that you can use to monitor observables remotely….more on it later.