A hobby project to track real-time live locations using WebSockets (Socket.io) and Apache Kafka for reliable message streaming.
- Clients (Browsers/Apps) connect to the Node.js server via Socket.io.
- Location Updates: Clients emit their location data to the server (
client:location:update). - Kafka Producer: The Node.js server acts as a Kafka producer, sending the received location data into the
location-updatesKafka topic. - Kafka Consumers:
- Socket Server Consumer: The server also consumes the
location-updatestopic and broadcasts the locations back to all connected clients via Socket.io (server:location:update). - Database Processor: A separate worker (
database-processor.js) independently consumes the samelocation-updatestopic to persist the location data into a database (currently mocked).
- Socket Server Consumer: The server also consumes the
This architecture ensures that high-volume location updates are reliably buffered, processed asynchronously, and broadcasted efficiently.
- Node.js (v18+ recommended)
- Docker and Docker Compose (for running Kafka)
Follow these steps to run the complete setup on your local machine:
npm installStart the Apache Kafka broker using Docker Compose:
docker compose up -dOnce Kafka is running, initialize the required topic (location-updates):
node kafka-admin.jsYou should see a message confirming that the Kafka admin connected and topics were created.
You will need to open two terminal windows to run both services:
Terminal 1: Start the Main Server This will start the Express web server and Socket.io handler.
npm run dev
# or
npm startThe server will start on port 5000 (default) and connect to Kafka.
Terminal 2: Start the Database Processor This simulates the worker that processes and saves locations to a database.
node database-processor.jsOnce everything is running:
- The server exposes a health check endpoint at
http://localhost:5000/health. - Any frontend files placed in the
public/directory will be served automatically athttp://localhost:5000/. - Connect your Socket.io client to
http://localhost:5000to start emitting and listening to location events!