A Node.js application demonstrating Server-Sent Events with message streaming capabilities. This project showcases two different streaming patterns: character-by-character streaming and progress-based streaming.
- Message Submission: Send messages via REST API and receive unique message IDs
- Character Streaming: Stream messages character by character with real-time progress
- Progress Streaming: Simulate processing with percentage-based progress updates
- Web Interface: Two HTML demos to interact with the streaming endpoints
- CORS Support: Cross-origin requests enabled for frontend integration
├── backend/
│ ├── src/
│ │ └── index.js # Main server application
│ ├── public/
│ │ ├── index.html # Character streaming demo
│ │ └── progress.html # Progress streaming demo
│ └── package.json # Dependencies and scripts
└── frontend/ # Frontend directory (placeholder)
Submit a message for processing and receive a unique message ID.
Request Body:
{
"message": "Your message here"
}Response:
{
"status": "ok",
"messageId": "uuid-string"
}Stream the message character by character with position tracking.
Response Format (SSE):
{
"messageId": "uuid-string",
"chunk": "character",
"position": 0,
"total": 100
}Stream with simulated processing progress (1-100%).
Response Format (SSE):
{
"messageId": "uuid-string",
"completed": false,
"chunk": "Processing chunk 50%",
"total": 100
}- Navigate to the backend directory:
cd backend- Install dependencies:
npm install- Start the development server:
npm start-
Open your browser and navigate to:
- Character streaming demo:
http://localhost:4400/index.html - Progress streaming demo:
http://localhost:4400/progress.html
- Character streaming demo:
-
Enter a message and click "Send Message" to see the streaming in action.
- express: Web framework for Node.js
- cors: Enable Cross-Origin Resource Sharing
- nodemon: Development dependency for auto-restarting the server
- Message Submission: Client sends a message to
/sendendpoint - ID Generation: Server generates a unique UUID for the message
- Stream Initiation: Client connects to the appropriate streaming endpoint
- Real-time Streaming: Server sends data chunks via Server-Sent Events
- Connection Management: Automatic cleanup on client disconnect
The server runs on port 4400 and includes:
- Automatic server restart with nodemon
- Memory-based message storage
- Active stream tracking
- Graceful connection cleanup
Server-Sent Events are supported in all modern browsers. The EventSource API is used for establishing SSE connections.