A demonstration of Server-Sent Events (SSE) implementation using Spring Boot, showcasing real-time event streaming to web clients.
- Real-time Event Streaming: Server-sent events via HTTP SSE protocol
- Multiple Event Types: Heartbeat, notifications, and custom events
- Concurrent Connections: Thread-safe handling of multiple SSE clients
- Interactive Web Client: HTML test interface for SSE demonstration
- Hot Reload: Spring Boot DevTools for development efficiency
- Java 17 or higher
- No additional installation required (uses Gradle wrapper)
- Clone and navigate to the project directory
- Start the application:
./gradlew bootRun
- Open your browser to
http://localhost:8080/test.html
GET /api/events
Content-Type: text/event-stream
Establishes an SSE connection and streams real-time events.
POST /api/trigger-event
Content-Type: application/json
Body: "Your custom message"
Broadcasts a custom event to all connected SSE clients.
GET /api/
Returns server status message.
- connected: Sent when a client first connects
- heartbeat: Periodic server heartbeat (every 30 seconds)
- notification: Sample notifications (every 15 seconds)
- custom: User-triggered events via POST endpoint
# Stream SSE events (press Ctrl+C to stop)
curl -N http://localhost:8080/api/events
# Send custom event
curl -X POST -H "Content-Type: application/json" \
-d '"Hello from curl!"' \
http://localhost:8080/api/trigger-event
Visit http://localhost:8080/test.html
for an interactive web interface that demonstrates:
- Connecting/disconnecting from SSE stream
- Real-time event display
- Sending custom events
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Web Client │────│ EventController │────│ EventService │
│ │ │ │ │ │
│ EventSource API │◄───│ SseEmitter │◄───│ Periodic Tasks │
└─────────────────┘ └─────────────────┘ └─────────────────┘
- EventController: REST endpoints for SSE and event triggering
- EventService: Connection management and event broadcasting
- SseEmitter: Spring's SSE implementation for streaming events
./gradlew build # Build the application
./gradlew test # Run tests
./gradlew bootRun # Run development server
- Port: 8080 (configurable in
application.properties
) - CORS: Enabled for all origins (development mode)
- DevTools: Automatic restart and live reload enabled
This example demonstrates SSE patterns suitable for:
- Real-time notifications
- Live dashboards
- Chat applications
- System monitoring displays
- Live data feeds
This project is provided as an educational example.