WebSocket . Jul 2024

WebSocket with Spring Framework

Share this Article

twitterlogofacebooklogolinkedinlogo
Introduction

WebSockets is a bidirectional, full-duplex, persistent connection between a web browser and a server. Once a WebSocket connection is established, the connection stays open until the client or server decides to close this connection.

websocket
WebSocket and STOMP Protocols

The WebSocket protocol allows you to implement bidirectional communication between applications. It is important to know that HTTP is used only for the initial handshake. After it happens, the HTTP connection is upgraded to a newly opened TCP/IP connection that is used by a WebSocket.

The WebSocket protocol is a rather low-level protocol. It defines how a stream of bytes is transformed into frames. A frame may contain a text or a binary message. Because the message itself does not provide any additional information on how to route or process it, It is difficult to implement more complex applications without writing additional code. Fortunately, the WebSocket specification allows the use of sub-protocols that operate on a higher, application level. One of them, supported by the Spring Framework, is STOMP.

STOMP is a simple text-based messaging protocol that was initially created for scripting languages such as Ruby, Python, and Perl to connect to enterprise message brokers. STOMP, clients and brokers developed in different languages can send and receive messages to and from each other. The WebSocket protocol is sometimes called TCP for Web. Analogically, STOMP is called HTTP for Web. It defines a handful of frame types that are mapped onto WebSockets frames, e.g., CONNECT, SUBSCRIBE, UNSUBSCRIBE,ACK, or SEND. On one hand, these commands are very handy to manage communication while, on the other, they allow us to implement solutions with more sophisticated features like message acknowledgment.

Message Configuration

WebSocket in java the setup with spring-boot-starter-websocket dependency can be implemented starting with Configuration.

spring boot
  • Message Broker Configuration

1. Creates the in-memory message broker with one or more destinations for sending and receiving messages. In the example above, there are two destination prefixes defined: topic and queue. They follow the convention that destinations for messages to be carried on to all subscribed clients via the pub-sub model should be prefixed with topic. On the other hand, destinations for private messages are typically prefixed by queue.

message

2. Defines the prefix app that is used to filter destinations handled by methods annotated with @MessageMapping which you will implement in a controller. The controller, after processing the message, will send it to the broker.

WebSocket Session Manager

The sessions are managed by the connection event listening

  • Session Active Management

1. When the Connection establishes the methods with the annotation of EventListener receive SessionConnectEvent in its listener

2. When the Connection is disconnected while the manual disconnection from frontend or tab was closed with the annotation of EventListener receive SessionDiconnectEvent in its listener.

Evenlistener
User Message Management
  • Message sending

The sending message from the application can be accessed through MessageMapping endpoints for various kinds of messaging service.

Message

Private message will be received for a specific user sending it with a private endpoint with a user id and Public message will be received for all users.

override
Pirai Infotech at the Forefront: AI in Action for Transportation
Ready to take your business to the next level?

Contact us today for a free consultation

Divider Image
+91 8015148627
Picture of the author

Recent Articles:

Accelerate Your Success
With Us

Pirai Enquiry Form
Phone

Subject