Blog
How I used websockets to make a chat app in Java
What are websockets?
WebSockets are a communication protocol that enables real-time, two-way communication between a client (typically a web browser) and a server over a single, long-lived connection. It provides a persistent connection that allows both the client and the server to send data to each other at any time without the need for the client to send a request each time.
...Why Websockets and not HTTP?
In HTTP goes in one direction. The client makes a request and the server responds. With WebSockets both client and server can send a message to the other. HTTP tends to be more resource intensive on servers.
How I used WebSockets with Java?
I used the Spring Websocket module because we were already using the Spring framework in the project. I also used the STOMP (Simple Text Oriented Messaging Protocol) messaging protocol which operates on top of the lower-level Websocket. As a fallback option I used SockJS for instances where WebSockets are not supported.
The backend code explained
I used the Spring Websocket module because we were already using the Spring framework in the project. I also used the STOMP (Simple Text Oriented Messaging Protocol) messaging protocol which operates on top of the lower-level Websocket. As a fallback option I used SockJS for instances where WebSockets are not supported.
The configuration
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements
WebSocketMessageBrokerConfigurer {
@Override
public void >registerStompEndpoints >(StompEndpointRegistry registry)
{
registry.addEndpoint("/ws" >).setAllowedOriginPatterns("*"
).withSockJS(); }
@Override
public void >configureMessageBroker (MessageBrokerRegistry registry)
{
registry.setApplicationDestinationPrefixes ("/app" );
registry.enableSimpleBroker( "/chatroom","/user" );
registry.setUserDestinationPrefix("/user");
} }
The configureMessageBrokermethod configures the
message broker, which handles message routing and broadcasting.
registry.setApplicationDestinationPrefixes("/app")
sets the prefix for messages that are bound for methods annotated with
@MessageMapping on the server side. In this case,
messages sent to destinations starting with appwill be routed to
annotated message-handling methods.
registry.enableSimpleBroker("/chatroom", "/user") enables a simple in-memory message broker. It specifies the prefixes
for destinations that the server can send messages to clients.
Messages sent to destinations starting with chatroom will be
broadcasted to all connected clients, and messages sent to
destinations starting with /user will be targeted to
individual users.
registry.setUserDestinationPrefix("/user")sets the
prefix for destinations that are used for direct user-to-user
communication. Messages sent to destinations starting with user will
be sent to specific user destinations.
This configuration class sets up the necessary endpoints, message
routing prefixes, and message broker configuration for WebSocket
communication using the STOMP protocol in Spring.