Building Persistable One-to-One Chat Application using Spring Boot and WebSockets



we will study a popular use case of WebSockets. We will be building a one-to-one messaging application, which allows users to send private messages to each other.

The below video demonstrates what we are going to build.


Introduction to WebSockets and STOMP

A WebSocket is a protocol that allows a two-way communication between server and client. Unlike HTTP, an application layer protocol, WebSocket is a transport layer (TCP) protocol. However, HTTP is used for the initial handshake, then it is upgraded to TCP connection that is used by WebSockets. WebSockets is a low level protocol, which defines the way of transfer messages with no additional information about the incoming messages.

For this reason, the WebSocket RFC defines the use of sub-protocols to define the structure and the standards for the messages. We will be using STOMP as a sub-protocol over WebSockets.

Simple (or Streaming) Text Oriented Message Protocol (STOMP) defines a protocol for server and client to communicate with messaging.

STOMP is similar to HTTP, and works over TCP using the following commands:

  • CONNECT

  • SUBSCRIBE

  • UNSUBSCRIBE

  • SEND

  • BEGIN

  • COMMIT

  • ACK



The Architecture


  • Auth Service is responsible for authenticating and managing users, we will not re-invent the wheel, we will the Auth service that we developed in the previous story.

  • Chat Service is responsible for configuring WebSocket, enabling STOMP message handling and, persisting and handling user messages.