
Modern applications need real-time capabilities — from chat apps and live dashboards to order tracking and alerts. Implementing a Spring Boot WebSocket notification system is one of the best ways to achieve this in the Java ecosystem, utilizing WebSockets with Spring Boot.
In this post, you’ll learn how to build a real-time notification system using Spring Boot and WebSocket, step by step. We’ll cover WebSocket configuration, broadcasting events, and sending messages to specific users.
🔌 What is WebSocket?
WebSocket is a full-duplex communication protocol over a single, long-lived TCP connection. Unlike HTTP, where the client must initiate each request, WebSocket allows the server to push data to clients in real time — making it ideal for:
- Notifications
- Live chat
- Activity feeds
- Stock/price updates
- Collaborative apps
⚙️ Step 1: Add WebSocket Dependencies
If you’re using Maven, add this to pom.xml
:
org.springframework.boot
spring-boot-starter-websocket
This brings in everything you need for STOMP-based messaging over WebSocket.
📡 Step 2: Configure WebSocket
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-notifications")
.setAllowedOriginPatterns("*")
.withSockJS(); // Fallback for older browsers
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic"); // Broadcast messages
registry.setApplicationDestinationPrefixes("/app");
}
}
- /ws-notifications: WebSocket connection endpoint
- /topic: Used for broadcasting messages
- /app: Used for sending messages from client to server
🧾 Step 3: Define a Notification Model
public class NotificationMessage {
private String title;
private String content;
// Constructors, getters, setters
}
📢 Step 4: Create a Notification Controller
@Controller
public class NotificationController {
@MessageMapping("/notify") // Listens to /app/notify
@SendTo("/topic/notifications")
public NotificationMessage broadcast(NotificationMessage message) {
return message;
}
}
Whenever a client sends a message to /app/notify
, Spring broadcasts it to everyone subscribed to /topic/notifications
.
🎯 Step 5: Client-Side Setup (JavaScript)
✅ This code lets any client send and receive messages in real time.
🧪 Test It
- Run your Spring Boot app.
- Open the HTML page in two browser tabs.
- Send a message from one tab.
- The other receives it instantly.
This simulates a real-time notification system across users/devices.
🛡️ Bonus: Sending Targeted Messages
To send messages to specific users or sessions:
- Enable
userDestinationPrefix
:
registry.setUserDestinationPrefix("/user");
- Use
SimpMessagingTemplate
to send directly:
@Autowired
private SimpMessagingTemplate messagingTemplate;
public void sendToUser(String username, NotificationMessage message) {
messagingTemplate.convertAndSendToUser(username, "/queue/notifications", message);
}
On the client, subscribe to /user/queue/notifications
.
📘 Related:
🧠 Final Thoughts
Combining Spring Boot and WebSocket gives you the tools to build powerful, real-time features — all within a robust, scalable Java backend. Whether you’re creating a dashboard, alert system, or chat app, this setup is efficient and production-ready.
From here, you can extend it with:
- ✅ Spring Security for user-based messages
- ✅ Redis or Kafka for distributed message brokers
- ✅ Persistent message history using JPA