Implements Server Sent Events (SSE) in a distributed system using Node.js
simple.js- Contains a simple implementation of SSE with EventEmittersingle-server.js- SSE implementation with BullMQ with EventEmittermulti-server.jsandmulti-server-worker.js- SSE implementation with BullMQ with Redis Pub/Sub
Note
This repository contains the full, runnable code examples for the accompanying blog post: Server-Sent Events (SSE) in Node.js: From Monoliths to Distributed Systems.
To run the code locally in your machine
-
Firstly, clone the repository into your local machine
git clone https://github.com/AlstonChan/nodejs-sse-distributed-system.git
-
Install the required dependency via
npmcd nodejs-sse-distributed-systemnpm install
-
Execute the script to run the server
npm run dev
Then open the client.html file to connect to the server to test SSE connection.
You should explore the package.json script section to see all the option to run the server.
To run the server with BullMQ implementation, you need a Redis server. You can run the docker compose file - compose.yml to run a Redis server via docker container.
docker compose -f "compose.yml" up