Features
Docs
CLI
Benchmarks
Examples

© 2024 MoroJs

Server-Sent Events

Real-time updates with Server-Sent Events (SSE) for live data streaming, notifications, and event-driven applications.

Real-Time Updates That Just Work

Stream live data to clients with Server-Sent Events.
Automatic reconnection, keep-alive heartbeats, and event-based messaging included.

It's This Simple

Enable SSE and stream events

typescript

1import { sse } from '@morojs/moro';
2
3// Enable SSE
4app.use(sse());
5
6// SSE endpoint
7app.get('/events', (req, res) => {
8  res.sse({
9    retry: 10000, // Reconnect after 10s
10    keepAlive: 30000 // Keep-alive every 30s
11  });
12  
13  // Send events
14  const interval = setInterval(() => {
15    res.sse.send({
16      event: 'update',
17      data: { timestamp: Date.now() }
18    });
19  }, 5000);
20  
21  // Cleanup on close
22  req.on('close', () => {
23    clearInterval(interval);
24  });
25});

Why SSE Matters

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

Why It Makes Sense

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

1import { sse } from '@morojs/moro';
2
3// Enable SSE
4app.use(sse());
5
6// SSE endpoint
7app.get('/events', (req, res) => {
8  res.sse({
9    retry: 10000, // Reconnect after 10s
10    keepAlive: 30000 // Keep-alive every 30s
11  });
12  
13  // Send events
14  const interval = setInterval(() => {
15    res.sse.send({
16      event: 'update',
17      data: { timestamp: Date.now() }
18    });
19  }, 5000);
20  
21  // Cleanup on close
22  req.on('close', () => {
23    clearInterval(interval);
24  });
25});

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

1import { sse } from '@morojs/moro';
2
3// Store connected clients
4const clients = new Set();
5
6app.get('/stream', (req, res) => {
7  res.sse();
8  clients.add(res);
9  
10  req.on('close', () => {
11    clients.delete(res);
12  });
13});
14
15// Broadcast to all clients
16app.post('/broadcast', (req, res) => {
17  clients.forEach(client => {
18    client.sse.send({
19      event: 'notification',
20      data: req.body
21    });
22  });
23  
24  return { sent: clients.size };
25});
26
27// Send different event types
28app.get('/events', (req, res) => {
29  res.sse();
30  
31  // Send different event types
32  res.sse.send({ event: 'message', data: 'Hello' });
33  res.sse.send({ event: 'update', data: { count: 10 } });
34  res.sse.send({ event: 'error', data: { message: 'Error' } });
35});

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

Related Features