Java WebSockets made simple with Javalin

WebSockets is a technology that enables establishing an interactive connection between the front-end and the service. Being an advanced web technology, it may appear intimidating. In this blog post, I will show you how to easily build a WebSockets enabled service with Java and Javalin.

A quick intro to WebSockets

WebSockets are a relatively new (2011), but a well-supported communication protocol. At the time of writing every major browser supports them.

What is so great about them? If you are looking to build a very interactive application (think Google Docs, chats or games) they are the protocol to choose. You get an open channel of communication, rather than having to rely on the request-response model.

How do you initiate a WebSocket connection in JavaScript? It is simple:

With the WsSession object wrapping Jetty’s Session object and adding convenience methods. The most useful one being session.send(“message”). The full list can be found with the official documentation: https://javalin.io/documentation#websockets

Making a simple service that receives and sorts a message

I wanted to build something very simple but fun to see as an example. I decided for a service that will receive the message from the frontend and then send back progressively more sorted version of the message. You can see the gif below illustrating the idea:

Smoothly sorting a phrase with WebSockets.

Writing this with request-response would be quite unpleasant as there would have to be quite a lot of polling involved. Imagine if we were dealing here with a similar blocking request with a chunked response. Real-time analytics streaming perhaps?

The service code is very simple with the most difficult part being the actual sorting:

More examples

Make sure to check them out for more details and inspiration. Also do not forget, that Javalin fully supports Kotlin.

Do I need to use Javalin when working with WebSockets?

You don’t have to use Javalin. You can use Spring, (with this good article by baeldung explaining how to) or multiple other frameworks.

I have used Javalin here as it provides a very good development experience. Once you understand how to work with WebSockets, you can use them in a less trivial frameworks with confidence.

Conclusion

WebSockets are an exciting technology that I think is not used enough. I believe this is partly because many developers are afraid of potential difficulties when developing with WebSockets. I hope this article gave you some more confidence to give WebSockets a try.

Post navigation

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About

E4developer is a place where I share my open and honest views on software development, technology and working with people. The name – e4 comes from a chess move, this is how I start most of my games. Follow me on twitter – @e4developer