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.
WebSocket keeps one TCP connection open after an HTTP upgrade, so the server can push data without the client polling.
| Technique | Direction | Good for |
|---|---|---|
| Polling | Client asks repeatedly | Rare updates, simplest setup |
| Server-sent events | Server to client only | Live feeds, notifications |
| WebSocket | Both directions | Chat, 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.
The starter includes the Spring WebSocket and messaging modules, which provide the STOMP support. There is no separate spring-boot-starter-messaging.
The original example omitted an import, so here is the complete class:
How the destinations work:
/app/... are routed to your @MessageMapping methods./topic/... are broadcast to every subscriber./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.
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.
To send messages from a scheduler, REST endpoint or event listener, inject SimpMessagingTemplate:
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://:
wss:// in production so traffic is encrypted.Principal is attached to the session and used for /user destinations.setAllowedOriginPatterns to your real origins.Upgrade and Connection headers and raise the idle timeout.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.
Rarely. All current browsers support WebSocket. Add .withSockJS() only if you must support clients behind proxies that block upgrades.
Replace the simple broker with a broker relay to RabbitMQ or ActiveMQ so every instance shares subscriptions and messages.
Use convertAndSendToUser and subscribe the client to /user/queue/....
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.