Skip to main content
Sonamu supports Server-Sent Events (SSE) based on the fastify-sse-v2 plugin. With SSE, you can push real-time data from the server to the client.

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

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

Default behavior:
  • Automatic SSE endpoint registration
  • Automatic reconnection support
  • Automatic keep-alive sending

SSE Plugin Options

Simple enable/disable

How SSE Works

Connection Flow

HTTP Headers

SSE uses special HTTP headers:
Characteristics:
  • text/event-stream: SSE-specific Content-Type
  • no-cache: Prevents caching
  • keep-alive: Maintains connection

Practical Configuration Examples

2. Development/Production Separation

Reason: In development, connections frequently disconnect due to HMR

3. Conditional Activation

Disabling Compression

SSE is a streaming response, so compression must be disabled.
Handled automatically in @stream decorator:

CORS Configuration

CORS configuration is required to use SSE from different domains.
Note: SSE can send authentication cookies, so 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.
Configuration values:
  • 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.
Key settings:
  • proxy_buffering off: Disable buffering (immediate transmission)
  • proxy_cache off: Disable caching
  • proxy_read_timeout 24h: Read timeout (long duration)

Debugging

Browser Developer Tools

curl Test

Options:
  • -N: Disable buffering (immediate output)

Important Notes

Important considerations when configuring SSE:
  1. Disable compression: SSE is streaming, so compression is prohibited (handled automatically by @stream decorator)
  2. Timeout settings: Maintain long connections
  3. CORS configuration: Required when using from different domains
  4. Reconnection handling: Clients need to implement auto-reconnection
  5. Browser limitations: Concurrent SSE connection limit (6 per domain)
    • Solution: Use HTTP/2 or reuse connections
  6. Proxy buffering: Must disable buffering in Nginx, etc.

Browser Support

SSE is supported in all modern browsers: IE 11 support: event-source-polyfill

Next Steps

Creating SSE Endpoints

Build SSE APIs with the @stream decorator

Client Integration

Using SSE in the frontend