Heartbeat Hotel – a Spring Boot application with WebSocket and STOMP

As an attempt to understand WebSocket and STOMP, I have come across several blog posts which have illustrated with code sample of how it works. Here I learnt shamelessly copied, pasted and modified their code and created a somehow contrived sample application with functionality remotely related to my current day job.


Blog posts I have come across and I found them to be very useful

So, What is it I am going to build?

This post is not intended to be a tutorial on WebSocket, STOMP or Spring Boot but rather a sample application I am trying to put up to be able to do:

  • allow two-way communication between client (browser) and server using WebSocket and STOMP
  • allow multiple clients with same identification to register itself with the server to receive notification from the server
  • allow heart beat message to be sent between client and server (although only server-to-client heart beat is implemented here)
  • allow client to de-register itself with the server

Where is the code?

It can be found on the github.

What is it really?

It is an application which allows client (browser) to establish connection with the server to be able to receive server heart beat message.

User can also use UI to notify server to “roll the date”. When date is rolled, server will send back a message with the new property date. All other users (with the same identification) will then also be notified with this message.

How to build the application?

This is a maven project with Java source level set to be 1.8

To build the project:

    mvn clean package

This will create a jar file under target/sample-1.0-SNAPSHOT.jar

How to run the application?

Since this is built by using Spring Boot, the generated jar file has everything that needs to run the application.

    java -jar target/sample-1.0-SNAPSHOT.jar

How to access from the browser?

  • http://localhost:8080
  • Enter the property name in the text box.
  • Click “Click to Connect” button to connect to the server
  • Click “Click to roll the date” button to tell the server to roll the date for this property
  • Click “Disconnect” button to de-register this client and disconnect from the server

What else is missing?

  • “in memory” message broker is being used in this application. Would love to extend it to ActiveMQ or RabbitMQ
  • The way how client de-registers itself with the server is kind of hacky. Before stomp client sends the “disconnect” message, a “disconnecting” call is made to the server to notify that the client is about to disconnect. When server discovers that there is no more client registered, it will no longer send heart beat message to clients with this identification. You can see it is not a very reliable or robust way of keeping track of clients. Improvement can be listening the “disconnect” message and “figure out” a way to identify which client is leaving (maybe tap into header information).
  • Another implementation of client (other than using javascript) in either Java or other language. A server to server communication is also a good candidate.

Final words

  • WebSocket provides a different way of looking at client-server communication compared to RESTful endpoint design. It would be interesting to discover scenarios and patterns using WebSocket.
  • As anyone can definitely tell, I am a lousy front end person.
Heartbeat Hotel – a Spring Boot application with WebSocket and STOMP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s