Protocols
Server-Sent Events
Real-time updates with Server-Sent Events (SSE) for live data streaming, notifications, and event-driven applications.
Overview
Without proper real-time communication, you're polling for updates, wasting bandwidth, and creating poor user experiences. With MoroJS, you get efficient one-way streaming automatically.
Traditional polling requires constant requests. SSE streams updates efficiently.
Without SSE
- Constant polling for updates
- Wasted bandwidth and resources
- Delayed updates
- Poor user experience
With MoroJS
- Efficient one-way streaming
- Automatic reconnection
- Real-time updates
- Built-in keep-alive heartbeats
One-Way Streaming
Perfect for server-to-client updates, notifications, and live data feeds.
Auto Reconnect
Automatic reconnection support with configurable retry intervals.
HTTP/1.1 Compatible
Works with standard HTTP connections. No special protocols required.
How It Works
Server-Sent Events (SSE) provides a simple way to stream data from server to client over HTTP. MoroJS includes built-in SSE middleware that handles connection management, automatic reconnection, and keep-alive heartbeats. Clients connect to an SSE endpoint and receive real-time updates as events.
Basic SSE Setup
MoroJS provides built-in Server-Sent Events middleware for real-time one-way communication from server to client.
Enable SSE
typescript
SSE Features
- One-way server-to-client communication
- Automatic reconnection support
- Keep-alive heartbeat
- Event-based messaging
- Broadcast to multiple clients
- HTTP/1.1 compatible
Advanced Usage
Broadcast to All Clients
typescript
Best Practices
Do
- Use SSE for one-way server updates
- Implement keep-alive heartbeats
- Clean up connections on close
- Use event types for different messages
- Handle reconnection gracefully
- Monitor client connections
Don't
- Use SSE for bidirectional communication
- Skip connection cleanup
- Send too many events too quickly
- Ignore client disconnections
- Use SSE for large data transfers
- Skip keep-alive heartbeats