TL;DR: Skip complex WebSocket setups for one-way real-time dashboards by using Server-Sent Events (SSE). This post demonstrates how to stream live metrics from an Express backend using
text/event-streamheaders andres.write(), and consume them in React using the nativeEventSourceAPI. You'll also learn critical production safeguards, like handlingreq.on('close')so timers and resources tied to closed connections do not leak in Node.js.
Key Takeaways
- Replace stateful WebSocket architectures with Server-Sent Events (SSE) for lightweight, one-way (Server → Client) data streaming.
- Configure Express endpoints with
Content-Type: text/event-streamandConnection: keep-aliveto hold the HTTP request open. - Include the
X-Accel-Buffering: 'no'header in your Node.js response to prevent reverse proxies (like Nginx) from buffering your real-time stream. - Push updates to the client using
res.write(), ensuring the payload is strictly formatted with thedata:prefix and double newlines (\n\n). - Always attach a
req.on('close')listener in Express to clear intervals and terminate processes when a user closes their tab, so per-connection resources do not leak. - Consume the SSE stream on the frontend by wrapping the browser's native
EventSourceAPI inside a custom React hook.
Imagine you are tasked with building a real-time analytics dashboard. The requirements are straightforward: stream live server metrics, user registrations, or financial data directly to an admin UI as the events happen.
Immediately, your mind jumps to WebSockets. You start installing socket.io, configuring Redis adapters for pub/sub across multiple Node.js instances, wrestling with sticky sessions on your load balancer, and writing custom heartbeat mechanisms to handle silent connection drops.
But think about the data flow. Your dashboard only reads data. The client never sends real-time messages back to the server. By choosing WebSockets, you have added bidirectional protocol handling, stateful scaling concerns, and extra infrastructure for what is a one-way data stream.
The solution to this architectural mismatch is Server-Sent Events (SSE).
SSE is a standard HTTP API that enables browsers to receive automatic updates from a server. Because it operates over standard HTTP, it works through most firewalls and proxies that already allow HTTPS (with buffering turned off, as covered below), and the browser reconnects automatically when the connection drops.
In this tutorial, we will build a real-time dashboard using a Node.js/Express backend and a React frontend, using SSE for a simple one-way data pipeline. We will also cover the critical production trade-offs you need to know before deploying this to a live environment.
The Problem with WebSockets for One-Way Data
WebSockets provide full-duplex, bidirectional communication over a persistent connection. They are perfect for chat applications, multiplayer games, and collaborative editing tools where both the client and server emit events continuously.
However, for a dashboard, data strictly flows from Server → Client. Using WebSockets here is like using a two-way radio when a simple PA speaker would do.
Server-Sent Events are built exactly for this. When a client requests an SSE endpoint, the server holds the HTTP request open and continuously pushes text data down the wire. The browser’s native EventSource API handles the stream.
To understand how lightweight this is, look at the raw HTTP response a server sends to initiate an SSE stream. It's just plain text over standard HTTP:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
data: {"cpu": 45, "memory": 60}
id: 1712880000000
event: metrics
data: {"cpu": 48, "memory": 62}
id: 1712880002000
event: metrics
Notice the headers: Content-Type: text/event-stream is what tells the browser to treat the response as an event stream. Each event is terminated by a blank line (\n\n), and the browser parses the fields for you. Note that events with an event: field (like metrics above) are delivered to addEventListener('metrics', ...), not to onmessage; only unnamed events reach onmessage. The server below sends unnamed events so the onmessage handler in our hook receives them. Let's implement this in a real Node.js application.
Building the Node.js / Express SSE Backend
To serve an SSE stream, our Node.js backend needs to do three things:
- Accept an HTTP GET request.
- Respond with the correct headers to keep the connection open.
- Periodically write stringified data to the response object (
res.write()) without closing the connection (res.end()).
Let's set up a basic Express server that simulates a stream of server metrics (CPU and Memory usage).
// server.js
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.get('/api/stream/metrics', (req, res) => {
// 1. Set the necessary headers for SSE
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
// Prevent reverse proxies from buffering the response
'X-Accel-Buffering': 'no',
});
// Send an initial payload immediately
res.write(`data: ${JSON.stringify({ message: 'Connected to metrics stream' })}\n\n`);
// 2. Simulate real-time data generation every 2 seconds
const intervalId = setInterval(() => {
const payload = {
timestamp: new Date().toISOString(),
cpu: Math.floor(Math.random() * 100),
memory: Math.floor(Math.random() * 100),
activeUsers: Math.floor(Math.random() * 1000),
};
// SSE format requires "data: [string payload]\n\n"
res.write(`data: ${JSON.stringify(payload)}\n\n`);
}, 2000);
// Comment lines (starting with ":") are ignored by EventSource but keep
// idle proxies and load balancers from timing the connection out
const heartbeatId = setInterval(() => res.write(': ping\n\n'), 15000);
// 3. Crucial: Handle client disconnects to prevent memory leaks
req.on('close', () => {
console.log('Client disconnected from stream');
clearInterval(intervalId);
clearInterval(heartbeatId);
res.end();
});
});
const PORT = process.env.PORT || 4000;
app.listen(PORT, () => {
console.log(`SSE Server running on http://localhost:${PORT}`);
});
Production Note: The
req.on('close')event listener is non-negotiable. If you omit this, Node.js will continue running thesetIntervalloop for every client that ever connected, even after they close their browser tab. Every writer that keeps running after disconnect holds memory and CPU, and with enough reconnecting clients the process will degrade or run out of memory.
Creating a Robust Custom React Hook for SSE
Now that our backend is broadcasting data, we need our React frontend to consume it. The browser provides a built-in API called EventSource for exactly this purpose.
However, integrating EventSource directly into React components can lead to messy useEffect blocks and overlapping connections if cleanup is not handled properly, especially in React's Strict Mode, which mounts effects twice in development.
To solve this, we will abstract the logic into a reusable custom hook. This hook handles the connection lifecycle, error states, and data parsing. Crucially, we use the useRef hook for our callback functions to ensure we don't accidentally tear down and recreate the SSE connection every time the parent component re-renders.
// hooks/useSSE.ts
import { useState, useEffect, useRef } from 'react';
interface SSEOptions {
url: string;
onMessage?: (data: any) => void;
onError?: (error: Event) => void;
}
export function useSSE<T>({ url, onMessage, onError }: SSEOptions) {
const [data, setData] = useState<T | null>(null);
const [isConnected, setIsConnected] = useState<boolean>(false);
// Use refs to store the latest callbacks without triggering re-connects
const onMessageRef = useRef(onMessage);
const onErrorRef = useRef(onError);
useEffect(() => {
onMessageRef.current = onMessage;
onErrorRef.current = onError;
}, [onMessage, onError]);
useEffect(() => {
const eventSource = new EventSource(url);
eventSource.onopen = () => {
setIsConnected(true);
console.log(`SSE Connected to ${url}`);
};
eventSource.onmessage = (event) => {
try {
const parsedData = JSON.parse(event.data);
setData(parsedData);
if (onMessageRef.current) onMessageRef.current(parsedData);
} catch (err) {
console.error('Error parsing SSE data:', err);
}
};
eventSource.onerror = (error) => {
console.error('SSE Connection Error:', error);
setIsConnected(false);
if (onErrorRef.current) onErrorRef.current(error);
// EventSource natively attempts to reconnect.
// We don't need to manually recreate the instance here.
};
// Cleanup function: Close connection when component unmounts
return () => {
eventSource.close();
setIsConnected(false);
};
}, [url]);
return { data, isConnected };
}
This hook opens one connection per mounted component (Strict Mode briefly opens and closes an extra one in development), parses the JSON payload defensively, and closes the connection when the user navigates away from the dashboard.
Building the Dashboard UI
With our custom hook ready, consuming the stream in a component becomes declarative. We can now focus strictly on the presentation layer.
Whether you are building internal admin tools or complex full-stack web applications for enterprise clients, keeping your data fetching logic decoupled from your UI makes the system easier to maintain.
Let's build a RealTimeDashboard component that displays our live server metrics.
// components/RealTimeDashboard.tsx
import React, { useState, useCallback } from 'react';
import { useSSE } from '../hooks/useSSE';
interface ServerMetrics {
timestamp: string;
cpu: number;
memory: number;
activeUsers: number;
}
export default function RealTimeDashboard() {
// We maintain a history array to potentially draw a chart
const [metricsHistory, setMetricsHistory] = useState<ServerMetrics[]>([]);
const handleNewMetrics = useCallback((newData: ServerMetrics) => {
if (newData.cpu !== undefined) {
setMetricsHistory((prev) => {
const updated = [...prev, newData];
// Keep only the last 10 events in memory to prevent UI lag
return updated.length > 10 ? updated.slice(1) : updated;
});
}
}, []);
const { data: currentMetrics, isConnected } = useSSE<ServerMetrics>({
url: 'http://localhost:4000/api/stream/metrics',
onMessage: handleNewMetrics,
});
return (
<div className="p-8 max-w-4xl mx-auto font-sans">
<div className="flex justify-between items-center mb-8">
<h1 className="text-3xl font-bold">Live Server Status</h1>
<div className="flex items-center gap-2">
<span
className={`w-3 h-3 rounded-full ${isConnected ? 'bg-green-500' : 'bg-red-500 animate-pulse'}`}
/>
<span className="text-sm text-gray-600">
{isConnected ? 'Connected' : 'Reconnecting...'}
</span>
</div>
</div>
<div className="grid grid-cols-3 gap-6 mb-8">
<StatCard title="CPU Usage" value={`${currentMetrics?.cpu || 0}%`} />
<StatCard title="Memory" value={`${currentMetrics?.memory || 0}%`} />
<StatCard title="Active Users" value={currentMetrics?.activeUsers || 0} />
</div>
<div className="bg-gray-50 p-6 rounded-lg border">
<h2 className="text-xl font-semibold mb-4">Event Log</h2>
<div className="space-y-2 h-64 overflow-y-auto">
{metricsHistory.map((metric, i) => (
<div key={i} className="text-sm font-mono text-gray-700 bg-white p-2 border rounded">
[{new Date(metric.timestamp).toLocaleTimeString()}] CPU: {metric.cpu}% | RAM:{' '}
{metric.memory}% | Users: {metric.activeUsers}
</div>
))}
</div>
</div>
</div>
);
}
function StatCard({ title, value }: { title: string; value: string | number }) {
return (
<div className="bg-white p-6 rounded-lg border shadow-sm">
<h3 className="text-gray-500 text-sm font-medium uppercase tracking-wider">{title}</h3>
<p className="text-4xl font-bold mt-2 text-gray-900">{value}</p>
</div>
);
}
Notice how we use functional state updates setMetricsHistory((prev) => ...) inside our callback. This prevents stale closures in React, ensuring we always append new data to the most current version of our array without having to add metricsHistory to our dependency arrays (which would otherwise trigger constant re-renders).
Preparing SSE for Production (The Gotchas)
While Server-Sent Events are simple, there are three production "gotchas" you must account for before deploying your dashboard.
1. Load Balancer Buffering (Nginx)
By default, reverse proxies like Nginx buffer HTTP responses. They wait until the backend has generated a sizable chunk of data before forwarding it to the client. Because an SSE stream never ends, Nginx will silently hold onto your events, and your React app will receive nothing for minutes, followed by a burst of delayed events.
You must explicitly disable buffering for SSE endpoints. In your Nginx configuration block:
# /etc/nginx/sites-available/api
location /api/stream/ {
proxy_pass http://node_backend;
# Disable buffering for Server-Sent Events
proxy_buffering off;
proxy_cache off;
# Keep the connection open for a long time
proxy_read_timeout 24h;
# Use HTTP/1.1 to the upstream and clear the default "Connection: close"
# header so the upstream connection can stay open
proxy_http_version 1.1;
proxy_set_header Connection '';
}
(Note: Nginx honours an X-Accel-Buffering: no response header from the upstream, as set in the backend code, unless it is listed in proxy_ignore_headers. Setting proxy_buffering off explicitly for the streaming location is a safer fallback.)
2. The HTTP/1.1 Connection Limit
This is the most important limitation of SSE. Over HTTP/1.1, browsers limit concurrent connections per browser and domain to a low number (6 in current browsers), and that limit is shared across all tabs.
If a user opens 6 tabs of your dashboard, the browser will consume all available connection slots. If they attempt to open a 7th tab, or even make a standard REST API request to that same domain, the request will queue until one of the SSE connections is closed.
The Solution: You must serve your application over HTTP/2. HTTP/2 uses multiplexing, carrying many requests and SSE streams over a single TCP connection. The maximum number of concurrent streams is negotiated between client and server (commonly 100 by default), which is far more headroom than six connections. When deploying your Node app behind an AWS Application Load Balancer or Nginx, ensure HTTP/2 is enabled on the edge proxy.
3. Authentication with EventSource
The native EventSource browser API does not allow you to pass custom headers (like Authorization: Bearer <token>). If your dashboard requires a JWT, you have two options:
- Use cookie-based auth:
EventSourcesends cookies automatically on same-origin requests, and on cross-origin requests when created withnew EventSource(url, { withCredentials: true })(the server must then return the matching CORS credential headers). AnHttpOnly,Secure,SameSitesession cookie is usually the simplest option. - Use a fetch-based client: Instead of the native
EventSource, use a library like@microsoft/fetch-event-source, which reads the stream withfetch()and lets you set headers. Be aware that this package has not had a release since version 2.0.1 (2021), so review it before depending on it, or write a smallfetchplusReadableStreamreader yourself.
Avoid putting a long-lived JWT in the query string (/api/stream?token=...). URLs end up in access logs, proxy logs and browser history. If you must use a URL parameter, make it a short-lived, single-use ticket issued by an authenticated endpoint.
// Example using fetch-event-source instead of native EventSource
import { fetchEventSource } from '@microsoft/fetch-event-source';
await fetchEventSource('http://localhost:4000/api/stream/metrics', {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
Accept: 'text/event-stream',
},
onmessage(ev) {
console.log(ev.data);
},
});
Final Thoughts
WebSockets remain the right tool for highly interactive, bidirectional applications. But for admin panels, monitoring dashboards, CI/CD status trackers, and live news feeds, Server-Sent Events offer a simpler architecture.
By using plain HTTP streams, you avoid a separate protocol, reuse your existing HTTP infrastructure (with buffering disabled on streaming routes), and keep your Node.js backend small.
Sources
- MDN: Using server-sent events
- WHATWG HTML Standard: Server-sent events
- Nginx: ngx_http_proxy_module (proxy_buffering, X-Accel-Buffering)
- @microsoft/fetch-event-source on GitHub
Need help building this in production?
SoftwareCrafting is a full-stack dev agency - we ship fast, scalable React, Next.js, Node.js, React Native & Flutter apps for global clients.
Get a Free ConsultationFrequently Asked Questions
Why should I use Server-Sent Events (SSE) instead of WebSockets for a dashboard?
For dashboards, data typically flows in one direction: from the server to the client. SSE is a lightweight, standard HTTP API designed specifically for one-way data streaming, avoiding the separate protocol, bidirectional message handling and extra scaling setup that WebSockets usually bring.
What HTTP headers are required to implement an SSE endpoint in Express?
To keep the connection open and format the stream correctly, you must set Content-Type: text/event-stream, Cache-Control: no-cache, and Connection: keep-alive. Additionally, setting X-Accel-Buffering: no is recommended to prevent reverse proxies from buffering the real-time response.
How do I prevent memory leaks when using SSE in a Node.js backend?
You must listen for the close event on the HTTP request object (req.on('close', ...)). When the client disconnects or closes their browser tab, use this event listener to clear any active intervals and call res.end() to finish the response.
Does SSE require heartbeats or proxy configuration?
Some, but less than WebSockets. The browser's EventSource reconnects automatically, so you do not write reconnection logic. You should still send a periodic comment line (: ping) so idle timeouts on proxies and load balancers do not close the stream, and you must disable response buffering on reverse proxies such as Nginx.
Can SSE handle two-way communication between the client and server?
No, Server-Sent Events are strictly for one-way communication pushing data from the server down to the client. If your application requires full-duplex, bidirectional communication like a chat app or multiplayer game, you should use WebSockets instead.
