feat(m3): Socket.io Real-time Layer & Notification Service#15
feat(m3): Socket.io Real-time Layer & Notification Service#15codeRisshi25 merged 2 commits intomainfrom
Conversation
- Add packages/notifications workspace with NotificationChannel abstraction - SocketChannel emits via Socket.io + Redis adapter (cross-process support) - NotificationService with notifyDriver/notifyRider/notifyRide helpers - Designed for future SMS/push channels - Add SOCKET_EVENTS constants to packages/common (ride:offer, ride:offer-expired, ride:accepted, ride:started, ride:completed, ride:cancelled, ride:driver-location, ride:otp) - Add Socket.io server to api-gateway - Refactor index.ts: http.createServer + server.listen (same port as REST) - sockets/index.ts: init with Redis adapter for horizontal scalability - sockets/auth.ts: JWT middleware (auth.token and Bearer header) - sockets/handlers/driver.ts: go-online, go-offline, location-update - sockets/handlers/rider.ts: subscribe/unsubscribe driver location (with trip ownership validation) - Install socket.io, @socket.io/redis-adapter in api-gateway - Add vitest.config.ts to scope tests to src/ only Tests: 14 passed (5 auth, 4 driver handler, 5 rider handler) TypeScript: api-gateway OK, notifications OK Closes #8
ebd3e9c to
d208947
Compare
|
Caution Review failedThe pull request is closed. ℹ️ Recent review infoConfiguration used: defaults Review profile: CHILL Plan: Pro ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (20)
📝 WalkthroughWalkthroughThis pull request introduces Socket.IO real-time communication to the API gateway with JWT authentication, role-based event handlers for drivers and riders, and a new shared notification service package. The Socket.IO server is attached to an HTTP server, uses Redis adapter for horizontal scalability, and integrates with a notification abstraction layer. Event constants are centralized in the common package. Changes
Sequence DiagramsequenceDiagram
participant Client as WebSocket Client
participant Gateway as api-gateway<br/>Socket.IO Server
participant Auth as JWT Auth<br/>Middleware
participant Handler as Handler<br/>(Driver/Rider)
participant Service as Driver/Rider<br/>Service
participant Prisma as Prisma<br/>Database
participant Redis as Redis Adapter
Client->>Gateway: Connect with auth token
Gateway->>Auth: socketAuthMiddleware()
Auth->>Auth: Extract token from handshake
Auth->>Auth: verifyToken()
Auth->>Gateway: Attach socket.data.user
alt Authentication Success
Gateway->>Handler: registerHandlers()
Handler->>Gateway: Auto-join personal room
Handler->>Gateway: Listen for events
else Authentication Failure
Gateway->>Client: Reject connection
end
Client->>Gateway: Emit event (e.g., driver:go-online)
Gateway->>Handler: Route to handler
Handler->>Service: Call service method
Service->>Prisma: Update database
Prisma-->>Service: Acknowledge
Service-->>Handler: Return result
Handler->>Gateway: Emit acknowledgment
Gateway->>Redis: Publish via Redis adapter
Redis->>Client: Deliver event
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
Overview
Implements Issue #8 — adds Socket.io real-time WebSocket communication and a
packages/notificationsabstraction layer for cross-process event delivery.Changes
packages/common— Socket Events Catalogevents/index.tswith 8SOCKET_EVENTSconstants (ride:offer,ride:accepted,ride:driver-location, etc.)packages/notifications— New WorkspaceNotificationChannelinterface (pluggable: SocketChannel now, SMS/push later)SocketChannelemits via Socket.io + Redis adapter (enables cross-process pub/sub from ride-worker in M4+)NotificationServicewith helpers:notifyDriver(),notifyRider(),notifyRide()createNotificationService(channels)factoryapps/api-gateway— Socket.io Serversrc/index.ts:http.createServer(app)+server.listen()(Socket.io shares same port as REST)sockets/index.ts: Socket.io init, Redis adapter for horizontal scalabilitysockets/auth.ts: JWT middleware (acceptsauth.tokenorAuthorization: Bearer)sockets/handlers/driver.ts:driver:go-online,driver:go-offline,driver:location-updatesockets/handlers/rider.ts:rider:subscribe-driver-location(with trip ownership validation),rider:unsubscribe-driver-locationRoom Strategy
driver:{driverId}— personal room, auto-joined on connectrider:{riderId}— personal room, auto-joined on connectride:{tripId}— shared room, rider joins to get live locationTests
14 new tests added:
sockets/__tests__/auth.test.ts— 5 tests (no token, invalid, valid via auth.token, Bearer fallback, malformed header)sockets/__tests__/driver-handler.test.ts— 4 tests (room join, go-online, go-offline, location-update)sockets/__tests__/rider-handler.test.ts— 5 tests (room join, subscribe with/without ownership, unsubscribe)Verification
Depends on
Blocks
notifyDriver()to send ride offers)ride:{tripId}room)Closes #8
Summary by CodeRabbit
Release Notes
New Features
Tests
Chores