Production-ready Spring Boot application for real-time, user-targeted notifications over WebSocket/STOMP. Features user-specific queues, simple subscription APIs, and a minimal HTML client for testing.
- Real-time server-to-client push using WebSocket/STOMP
- Per-user delivery via
/user/queue/notifications - Simple REST endpoints to manage subscriptions and publish channel events
- MongoDB integration (repositories defined, persistence toggled in code)
Entry point: com.developersuraj.realtimenotifications.RealTimeNotificationsApplication
- WebSocket/STOMP config:
com.developersuraj.realtimenotifications.config.WebSocketConfig- Endpoint:
/ws(SockJS enabled) - Broker:
/topic,/queue - User destination prefix:
/user - Principal mapping via STOMP
loginheader
- Endpoint:
- REST controllers:
- Services:
- Data layer:
- DTOs:
com.developersuraj.realtimenotifications.dto.SubscriptionRequestcom.developersuraj.realtimenotifications.dto.ChannelEventcom.developersuraj.realtimenotifications.dto.NotificationDtocom.developersuraj.realtimenotifications.dto.Subscriptioncom.developersuraj.realtimenotifications.dto.Nofiticationcom.developersuraj.realtimenotifications.dto.Channelcom.developersuraj.realtimenotifications.dto.User
Client UI: src/main/resources/static/index.html
Config: src/main/resources/application.properties
Build: pom.xml
- Java 21
- MongoDB running at
localhost:27017(default DB:test) - Port
8080available
-
Install dependencies offline:
./mvnw -q dependency:go-offline
-
Configure application properties:
# Windows (PowerShell / CMD)
./mvnw.cmd spring-boot:run
# macOS/Linux
./mvnw spring-boot:runAccess demo UI: http://localhost:8080/index.html
Or open src/main/resources/static/index.html directly.
- The client connects to
/ws(SockJS) and sets STOMPloginheader to the numeric userId. com.developersuraj.realtimenotifications.config.WebSocketConfigmaps that header to aPrincipal.- Server sends per-user messages via
NotificationService.convertAndSendToUser. - Client listens on
/user/queue/notifications.
-
Subscribe a user to a channel:
# POST /api/subscriptions/subscribe curl -X POST http://localhost:8080/api/subscriptions/subscribe \ -H "Content-Type: application/json" \ -d '{"userId":1,"channelId":42}'
Payload:
com.developersuraj.realtimenotifications.dto.SubscriptionRequest -
Publish a channel event (broadcast to subscribers):
# POST /api/notifications/publish curl -X POST http://localhost:8080/api/notifications/publish \ -H "Content-Type: application/json" \ -d '{"channelId":42,"videoId":"abc123","videoTitle":"Intro to WebSockets"}'
Payload:
com.developersuraj.realtimenotifications.dto.ChannelEvent
- Start the server.
- Open http://localhost:8080/index.html.
- Enter userId
1and click “Connect.” - Subscribe user
1to channel42:- Use the subscribe API (see above).
- Publish a channel event on
42:- Use the publish API.
- The UI should display:
"[Channel 42] New video: Intro to WebSockets".
- Basic context test:
com.developersuraj.realtimenotifications.RealTimeNotificationsApplicationTests
./mvnw test- Persistence for notifications is present but commented in
com.developersuraj.realtimenotifications.service.NotificationService. Enable to record delivery state. - Allowed origins for WebSocket are currently
*. Adjust incom.developersuraj.realtimenotifications.config.WebSocketConfig. - Mongo repositories:
- Authentication and session management
- Durable delivery and read receipts
- Topic-based broadcasting with fine-grained permissions
- Integration tests for WebSocket messaging