This project demonstrates real-time communication between a server and client using Server-Sent Events (SSE). It features a Node.js/Express backend that streams progress updates to a React frontend in real-time.
This example showcases:
- Server-Sent Events (SSE): A web standard that allows a server to push data to a client over a single HTTP connection
- Real-time progress tracking: The backend simulates a long-running task and sends progress updates to connected clients
- Event-driven architecture: Uses Node.js EventEmitter to handle progress events
- React frontend: Displays a live progress bar that updates automatically as the server sends progress data
┌──────────────────┐ SSE Connection ┌───────────────────┐
│ │ ──────────────────► │ │
│ React Frontend │ │ Express Backend │
│ (Port 5173) │ ◄────────────────── │ (Port 3000) │
│ │ ◄────────────────── │ │
└──────────────────┘ Progress Stream └───────────────────┘
│
▼
┌───────────────────┐
│ Task Worker │
└───────────────────┘
- Persistent connection: Single HTTP connection maintained for streaming updates
- Automatic reconnection: Client automatically reconnects if connection drops
- Progress visualization: Real-time progress bar with percentage updates
- Cross-origin support: CORS enabled for frontend-backend communication
- Event-driven updates: Clean separation of concerns using EventEmitter pattern
sse-example/
├── backend/
│ ├── server.js # Express server with SSE endpoint
│ ├── worker.js # Simulated long-running task
│ ├── progressEmitter.js # Event emitter for progress updates
│ └── package.json
├── frontend/
│ ├── src/
│ │ ├── App.jsx # React component with progress bar
│ │ └── main.jsx # React app entry point
│ ├── index.html
│ ├── package.json
│ └── vite.config.js
└── README.md
- Node.js (v14 or higher)
- npm or yarn
-
Clone the project repository
git clone https://github.com/wajedisleem/sse-example.git
-
Install backend dependencies
cd backend npm install -
Install frontend dependencies
cd ../frontend npm install
-
Start the backend server (Terminal 1):
cd backend node server.jsThe server will start on
http://localhost:3000and begin the simulated worker task. -
Start the frontend development server (Terminal 2):
cd frontend npm run devThe React app will start on
http://localhost:5173(or similar port shown in terminal). -
Open your browser and navigate to the frontend URL to see the real-time progress updates!
- A progress bar that updates in real-time from 0% to 100%
- Progress updates streaming from the server every 100ms
- The cycle repeats automatically when it reaches 100%
- Browser developer tools will show the EventSource connection in the Network tab
- Description: SSE endpoint for real-time progress updates
- Response: Text/event-stream with progress data
- Data format:
{ "moved": 45, "total": 100, "percent": "45.00" }
- Express.js: Web framework for Node.js
- CORS: Cross-origin resource sharing middleware
- EventEmitter: Node.js events for progress communication
- React: UI library for building the interface
- Vite: Fast build tool and development server
- EventSource API: Browser API for consuming server-sent events
This example demonstrates:
- SSE vs WebSockets: SSE is simpler for one-way server-to-client communication
- Event-driven patterns: Clean separation using EventEmitter
- Real-time updates: How to stream data without polling
- CORS handling: Enabling cross-origin requests for development
- Connection management: Handling client connections and cleanup
You can extend this example by:
- Adding multiple concurrent tasks with different progress streams
- Implementing user authentication for personalized progress
- Adding error handling and retry logic
- Storing progress in a database for persistence
- Adding WebSocket support for bidirectional communication