Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you implement Spring Boot WebSocket for real-time communication?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2024
UpdatedNov 16, 2026
5 min read

To add real-time communication to Spring Boot, add spring-boot-starter-websocket, enable STOMP messaging with @EnableWebSocketMessageBroker, register a /ws endpoint, and handle messages with @MessageMapping methods that broadcast to /topic destinations. Browsers connect once and then both sides can send messages at any time over the same connection.

This guide builds a small chat-style broadcast with a browser client, then covers the security settings that real deployments need. It applies to Spring Boot 3.x and 4.x with Java 17 or newer.

Table of Contents

  • When WebSocket is the right choice
  • Adding the dependency
  • Configuring the STOMP broker and endpoint
  • Handling messages in a controller
    • Pushing from anywhere in your application
  • Connecting from the browser
  • Securing real-time endpoints
  • Frequently Asked Questions
    • What is the difference between WebSocket and STOMP?
    • Do I still need SockJS?
    • How do I scale beyond one server instance?
    • How do I send a message to one user only?
  • Summary

When WebSocket is the right choice

WebSocket keeps one TCP connection open after an HTTP upgrade, so the server can push data without the client polling.

TechniqueDirectionGood for
PollingClient asks repeatedlyRare updates, simplest setup
Server-sent eventsServer to client onlyLive feeds, notifications
WebSocketBoth directionsChat, collaboration, games, live dashboards

If you only push data from the server, server-sent events are often simpler. Use WebSocket when clients also send frequent messages.

Adding the dependency

xml
groovy

The starter includes the Spring WebSocket and messaging modules, which provide the STOMP support. There is no separate spring-boot-starter-messaging.

Configuring the STOMP broker and endpoint

The original example omitted an import, so here is the complete class:

java

How the destinations work:

  • Messages sent to /app/... are routed to your @MessageMapping methods.
  • Messages published to /topic/... are broadcast to every subscriber.
  • Messages to /user/... go to a single authenticated user.
  • enableSimpleBroker is an in-memory broker. For several application instances, use a relay to RabbitMQ or ActiveMQ with enableStompBrokerRelay.

Restrict allowed origins. Allowing * lets any website open a socket as your logged-in users.

Handling messages in a controller

java

A client that sends to /app/chat.send triggers send, and the return value is published to /topic/messages for all subscribers. Records are deserialized from JSON by Jackson automatically.

Pushing from anywhere in your application

To send messages from a scheduler, REST endpoint or event listener, inject SimpMessagingTemplate:

java

Connecting from the browser

Use the STOMP client library for the browser, or a small wrapper such as @stomp/stompjs. Native browsers speak plain WebSocket, so connect to ws:// or wss://:

javascript
javascript
Loading syntax highlighting...

Securing real-time endpoints

  1. Always use wss:// in production so traffic is encrypted.
  2. Authenticate during the HTTP handshake. With Spring Security, the logged-in Principal is attached to the session and used for /user destinations.
  3. Authorise messages with Spring Security's message authorisation rules, not only URL rules.
  4. Validate payload size and content, and limit message rates, because a socket is a long-lived channel.
  5. Set setAllowedOriginPatterns to your real origins.
  6. Behind a proxy such as Nginx, forward the Upgrade and Connection headers and raise the idle timeout.

Frequently Asked Questions

What is the difference between WebSocket and STOMP?

WebSocket is the transport. STOMP is a simple text messaging protocol on top that adds destinations, subscriptions and headers, so you do not invent your own message format.

Do I still need SockJS?

Rarely. All current browsers support WebSocket. Add .withSockJS() only if you must support clients behind proxies that block upgrades.

How do I scale beyond one server instance?

Replace the simple broker with a broker relay to RabbitMQ or ActiveMQ so every instance shares subscriptions and messages.

How do I send a message to one user only?

Use convertAndSendToUser and subscribe the client to /user/queue/....

Summary

Add the WebSocket starter, configure a STOMP broker and a /ws endpoint, handle input with @MessageMapping, and publish with @SendTo or SimpMessagingTemplate. Restrict origins, use wss://, authorise messages, and move to a broker relay when you run more than one instance.

Share