A powerful, highly scalable real-time WebSocket server built with NestJS and Socket.IO. This system handles generic event broadcasting alongside a dedicated, in-memory human tracking engine designed to track active users' geolocation coordinates.
- Real-time Geofencing & Human Tracking Module
- Instant location tracking via
locationUpdateevents. - O(1) in-memory state tracking to safely map coordinates and dispatch updates globally.
- Smart disconnect handling that instantly broadcasts missing/disconnected clients to everyone else to avoid ghost nodes.
- Connect-time snapshots giving new users the exact location of all active trackers upon entry.
- Instant location tracking via
- Custom Socket Utilities
- Includes a custom-built
@Ack()parameter decorator to easily latch onto underlying trailing ACK callbacks provided natively by Socket.IO.
- Includes a custom-built
- Global CORS Configured
- Easily accepts cross-origin multi-client access, fully supporting
localhostorigins alongside Ngrok dev-tunnels testing.
- Easily accepts cross-origin multi-client access, fully supporting
The server is built using standard NestJS modular architecture, ensuring separation of concerns:
AppModule: Core root module mapping dependencies.ChatModule / ChatGateway: A lightweight gateway for sending string-based messages and testing custom parameter decorators.TrackingModule:TrackingGateway: Listens for location payloads, broadcasts changes to peers, and cleans up sockets when they vanish.TrackingService: In-memoryMap-driven repository enforcing logic to assign Socket IDs exclusively to exact user IDs, preventing memory leaks when sockets crash.
# Install all required dependencies
$ npm install# development
$ npm run start
# watch mode (highly recommended for live reloads)
$ npm run start:dev
# production mode
$ npm run start:prodOnce running, the application natively exposes Socket.IO on port 3000. You can connect using any Socket.IO client (like Postman or a React frontend).
| Event Name | Direction | Payload Example | Description |
|---|---|---|---|
initialLocations |
Server → Client | [{ userId: "u1", lat: 34.0, lng: -118.0, ... }] |
Fired when a client successfully connects. Returns an array of everyone's last known locations. |
locationUpdate |
Client → Server | { "userId": "uuid", "lat": 12.3, "lng": 45.6, "timestamp": 16000 } |
Used to report the client's current coordinates. |
locationBroadcast |
Server → Client | { "userId": "uuid", "lat": 12.3, "lng": 45.6, "timestamp": 16000 } |
Server echoes the locationUpdate coordinates back to all connected listeners. |
userDisconnected |
Server → Client | { "userId": "uuid" } |
Alerts the frontend that a user socket dropped, useful for removing map pins. |
events |
Client ↔ Server | "broadcasting..." |
Basic playground event found in ChatGateway using standard emit architectures. |
Ensure the platform you use supports HTTP Upgrades (WebSockets) natively. Recommended easy platforms include:
- Railway
- Render
- DigitalOcean App Platform
- Fly.io