Introduction
Rodando Backend uses Socket.io for real-time bidirectional communication between clients and the server. The WebSocket infrastructure is organized into multiple namespaces, each serving a specific user type or purpose.Architecture
The WebSocket implementation follows a clean, modular architecture:- Gateways: Handle connection/disconnection and message subscriptions
- Publishers: Emit events to clients in response to domain events
- Listeners: Subscribe to domain events and trigger publishers
- Adapters: Configure Socket.io server with CORS and optional Redis clustering
Namespaces
The system provides four separate Socket.io namespaces:| Namespace | Path | Purpose |
|---|---|---|
| Driver Auth | /drivers | Driver authentication and connection management |
| Driver Availability | /drivers | Driver status, location, and trip updates |
| Passenger | /passengers | Passenger trip status and driver updates |
| Admin | /admin | Administrative dashboards and monitoring |
The
/drivers namespace handles both authentication and availability events through separate gateways.Authentication
All WebSocket connections require JWT authentication. Tokens can be provided in two ways:Method 1: Auth Object (Recommended)
Method 2: Authorization Header
Connection Flow
- Client initiates connection with JWT token
- Gateway validates token using
TokenService.verifyAccessToken() - User and session verification:
- User exists and is active
- User type matches namespace (DRIVER for
/drivers, PASSENGER for/passengers) - Session is not revoked
- Access token has not expired
- Socket joins rooms (e.g.,
driver:{userId},session:{sid}) - Hello event emitted to confirm successful connection
- Connection rejected if any validation fails
Rooms
The system uses Socket.io rooms for targeted message delivery:session:{sid}- All sockets for a specific session (for forced disconnects)admin:all- All admin usersadmin:drivers- Admin dashboards monitoring drivers
Event Naming Conventions
Outbound (Server → Client)
trip:*- Trip lifecycle eventsdriver:*- Driver-specific eventsadmin:*- Admin dashboard eventsauth:*- Authentication events
Inbound (Client → Server)
driver:status:update- Update driver online/availability statusdriver:location:ping- Update driver GPS locationdriver:trip:set- Associate driver with tripdriver:offer:accept- Accept trip assignment offerdriver:offer:reject- Reject trip assignment offer
Error Handling
Connection errors are logged and the socket is immediately disconnected:Missing token- No JWT providedMissing sub/sid- Invalid JWT payloadUser not found- User ID from token doesn’t existWrong userType- User type doesn’t match namespaceUser inactive- User account is not activeInvalid session- Session not found or revokedAccess expired- Access token has expired
CORS Configuration
CORS is configured in the Socket.io adapter:WS_CORS_ORIGIN environment variable to restrict allowed origins:
Scaling with Redis
For multi-instance deployments, enable Redis adapter (currently commented out):Next Steps
Connection Setup
Learn how to establish WebSocket connections
Driver Auth
Driver authentication namespace events
Driver Availability
Driver status and location events
Passenger Events
Passenger trip lifecycle events
