# SSE Server (EventSource)

> Simple Server-Sent Events development server.

- Category: Message Queues & Brokers
- Difficulty: beginner
- Components: node
- Tags: sse, eventsource, realtime, streaming
- Source: https://docker.recipes/messaging/sse-server

## 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.

## 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

## Use cases

- Real-time dashboard updates for system monitoring and analytics visualization
- Live notification systems for social media applications and messaging platforms
- Financial market data streaming for trading platforms and investment tools
- IoT sensor data broadcasting to multiple connected devices and monitoring systems
- Live sports scores and game updates for sports applications and websites
- Server log streaming and debugging tools for development teams
- 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

## docker-compose.yml

```yaml
services:
  sse-server:
    image: node:20-alpine
    container_name: sse-server
    restart: unless-stopped
    working_dir: /app
    command: node server.js
    volumes:
      - ./app:/app
    ports:
      - "3000:3000"
```

## .env

```bash
# Create server.js with SSE endpoints
```

## Usage notes

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

## 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

