$docker.recipes

SSE Server (EventSource)

beginner

Simple Server-Sent Events development server.

[i]Overview

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine that enables server-side JavaScript execution. Originally created by Ryan Dahl in 2009, Node.js revolutionized web development by allowing developers to use JavaScript for both client and server-side code. Its event-driven, non-blocking I/O model makes it particularly well-suited for real-time applications and data-intensive tasks that require high concurrency. This Docker configuration creates a lightweight Server-Sent Events (SSE) development server using Node.js on Alpine Linux. The setup provides a minimal foundation for building real-time, one-way communication from server to client using the EventSource API. Unlike WebSockets which require bidirectional communication, this Node.js SSE server focuses specifically on pushing data from server to browser clients with automatic reconnection and built-in event handling. Developers building real-time dashboards, live notifications, or streaming data applications will find this configuration particularly valuable. The combination of Node.js's event loop architecture and SSE's simplicity creates an efficient solution for applications that need to push updates to multiple clients simultaneously without the complexity of full WebSocket implementations.

[*]Key Features

  • [+]Node.js 20 Alpine Linux base image for minimal resource footprint and enhanced security
  • [+]Built-in EventSource API support with automatic client reconnection on connection drops
  • [+]Non-blocking I/O event loop architecture optimized for concurrent client connections
  • [+]Native text/event-stream content type handling for proper SSE protocol compliance
  • [+]Automatic event ID sequencing and last-event-id tracking for reliable message delivery
  • [+]Hot-reload development environment with volume mounting for rapid iteration
  • [+]Cross-origin resource sharing (CORS) compatibility for browser-based clients
  • [+]Memory-efficient streaming without buffering large datasets

[#]Common Use Cases

  • [1]Real-time dashboard updates for system monitoring and analytics visualization
  • [2]Live notification systems for social media applications and messaging platforms
  • [3]Financial market data streaming for trading platforms and investment tools
  • [4]IoT sensor data broadcasting to multiple connected devices and monitoring systems
  • [5]Live sports scores and game updates for sports applications and websites
  • [6]Server log streaming and debugging tools for development teams
  • [7]Chat application message broadcasting and presence indicators

[!]Prerequisites

  • [!]Docker and Docker Compose installed on the host system
  • [!]Port 3000 available on the host machine for the SSE server
  • [!]Basic JavaScript and Node.js knowledge for server implementation
  • [!]Understanding of EventSource API and browser-based event handling
  • [!]Minimum 512MB RAM available for Node.js runtime and application code
  • [!]Local ./app directory with server.js file containing SSE implementation
[!]

WARNING: For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms

[$]docker-compose.yml

[docker-compose.yml]
1services:
2 sse-server:
3 image: node:20-alpine
4 container_name: sse-server
5 restart: unless-stopped
6 working_dir: /app
7 command: node server.js
8 volumes:
9 - ./app:/app
10 ports:
11 - "3000:3000"

[$].env Template

[.env]
1# Create server.js with SSE endpoints

[i]Usage Notes

  1. [1]Docs: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events
  2. [2]Development server at http://localhost:3000
  3. [3]Create server.js with res.setHeader('Content-Type', 'text/event-stream')
  4. [4]Browser: const es = new EventSource('/events'); es.onmessage = ...
  5. [5]Simpler than WebSocket for one-way server-to-client streaming
  6. [6]Auto-reconnection and event IDs built into protocol

[>]Quick Start

[terminal]
1# 1. Create the compose file
2cat > docker-compose.yml << 'EOF'
3services:
4 sse-server:
5 image: node:20-alpine
6 container_name: sse-server
7 restart: unless-stopped
8 working_dir: /app
9 command: node server.js
10 volumes:
11 - ./app:/app
12 ports:
13 - "3000:3000"
14EOF
15
16# 2. Create the .env file
17cat > .env << 'EOF'
18# Create server.js with SSE endpoints
19EOF
20
21# 3. Start the services
22docker compose up -d
23
24# 4. View logs
25docker compose logs -f

[>]One-Liner

Run this command to download and set up the recipe in one step:

[terminal]
1curl -fsSL https://docker.recipes/api/recipes/sse-server/run | bash

[?]Troubleshooting

  • [!]EventSource connection immediately closes: Ensure Content-Type header is set to 'text/event-stream' and Connection header is 'keep-alive'
  • [!]Browser shows 'net::ERR_INCOMPLETE_CHUNKED_ENCODING': Remove Content-Length header and use Transfer-Encoding: chunked for streaming responses
  • [!]Events not reaching client after server restart: Implement proper event ID tracking and handle last-event-id header in reconnection requests
  • [!]High memory usage with many clients: Implement proper stream cleanup and remove event listeners when clients disconnect
  • [!]CORS errors in browser console: Add appropriate Access-Control-Allow-Origin headers for cross-domain EventSource connections
  • [!]Container exits immediately: Verify server.js exists in ./app directory and contains valid Node.js code with proper event loop handling

Community Notes

Loading...
Loading notes...

## Download Recipe Kit

Get all files in a ready-to-deploy package

Includes docker-compose.yml, .env template, README, and license