Creating Raw Websockets in Spring Boot

date
Jan 24, 2024
slug
raw-websockets-spring
status
Published
tags
SOCKETS
summary
type
Post

Websockets

Websockets allows full bi directional communication between server and client. Basically, client and server both can send messages to one another.
To establish a websocket connections, a HTTP handsake takes place after TCP handsake to upgrade communication from HTTP to Web Socket protocal.
 
notion image
To keep connection alive, web sockets uses hearbeat ping/pong mechanism to keep connection alive.
 

Spring Websockets

  1. To create raw websockets in spring boot we need to implement WebSocketConfigurer interface
 
  1. WebSocketHandlerRegistry is also a interface that has a single method takes WebSocketHandler instance and paths.
 
  1. So we need to create a bean of type WebSocketHandler
  1. I will use extend TextWebSocketHandler that implements WebSocketHandler provided by Spring to implement handler.
  1. Finally we need to implement WebSocketConfigurer
 
  1. We can create a JS client to connect with server for testing.
OUPUT
notion image
Spring LOG
notion image
 
 
References
  1. Jorge Acetozi - Pro Java Clustering and Scalability_ Building Real-Time Apps with Spring, Cassandra, Redis, WebSocket and RabbitMQ (2017, Apress)
 

© Rupesh Dang 2021 - 2025