What is SSE?
Server-Sent Events is a technology that provides unidirectional real-time communication from server to client.HTTP vs SSE vs WebSocket
Comparison Table
| Feature | HTTP | SSE | WebSocket |
|---|---|---|---|
| Direction | Request→Response | Server→Client | Bidirectional |
| Protocol | HTTP | HTTP | WebSocket |
| Reconnection | X | Automatic | Manual |
| Complexity | Low | Medium | High |
| Use case | General API | Real-time push | Real-time chat |
SSE Use Cases
Real-time Notifications
New messages, likes, comments, etc.
Progress Updates
File uploads, task processing progress
Live Feeds
News feeds, social media updates
Monitoring
Server status, log streaming
Basic Setup
sonamu.config.ts
- Automatic SSE endpoint registration
- Automatic reconnection support
- Automatic keep-alive sending
SSE Plugin Options
- boolean
- SsePluginOptions
Simple enable/disable
How SSE Works
Connection Flow
HTTP Headers
SSE uses special HTTP headers:text/event-stream: SSE-specific Content-Typeno-cache: Prevents cachingkeep-alive: Maintains connection
Practical Configuration Examples
1. Basic Setup (Recommended)
2. Development/Production Separation
3. Conditional Activation
Disabling Compression
SSE is a streaming response, so compression must be disabled.CORS Configuration
CORS configuration is required to use SSE from different domains.credentials: true is required
Environment-specific Strategies
Development Environment
Production Environment
Timeout Configuration
SSE connections are maintained for long periods, so timeout configuration is important.connectionTimeout: 0: Disable connection timeout (SSE maintains long connections)keepAliveTimeout: Keep-alive interval (default: 5 seconds)
Proxy Configuration (Nginx)
When using Nginx, SSE-specific configuration is required.proxy_buffering off: Disable buffering (immediate transmission)proxy_cache off: Disable cachingproxy_read_timeout 24h: Read timeout (long duration)
Debugging
Browser Developer Tools
curl Test
-N: Disable buffering (immediate output)
Important Notes
Browser Support
SSE is supported in all modern browsers:| Browser | Support |
|---|---|
| Chrome | ✅ |
| Firefox | ✅ |
| Safari | ✅ |
| Edge | ✅ |
| IE 11 | ❌ (polyfill required) |