Live order tracking and rider management
6 modules · 7 lessons · 10 exercises · 17 available now
Week 5 overview
- Module 1 WebSocket hub
- 5.1 Channels, select and cleanupChannels, Buffered channel, Closing a channel, select statement
- 5.2 Managing WebSocket connectionsWebSocket, WebSocket hub, Slow consumer, WebSocket ping and heartbeat
- 5.3 Per-user WebSocket hub
- Module 2 Redis event fan-out
- 5.4 Redis pattern subscriptionsRedis pub/sub, Pub/sub fan-out across instances
- 5.5 Per-user order event bridge
- Module 3 Rider management and assignment
- 5.6 Redis GEO and SET NX PXRedis GEO, GEOSEARCH nearest query, Exclusive claim with SET NX PX
- 5.7 Rider presence service
- 5.8 Rider assignment worker
- Module 4 Live rider tracking
- 5.9 Watching foreground locationForeground location permission, watchPositionAsync, MapView and Marker
- 5.10 Reconnecting WebSocket hooksReconnecting WebSocket, Effect cleanup, Handler ref
- 5.11 Order location stream
- 5.12 Rider mode and customer tracking screens
- 5.13 Hub broadcast and claim race tests
- Module 5 Live capacity planning
- 5.14 Capacity estimationMemory per connection, File descriptor limit
- Module 6 Live order extensions
- 5.15 Per-user Redis replay log (optional)
- 5.16 Topic subscription registry (optional)
- 5.17 Rider map geometry library (optional)
Weekly learning objectives
Week 4 gave you an app that polls for order status. This week, order events reach the phone over a WebSocket, riders report their location, the nearest rider gets an exclusive claim on a READY order, and the customer watches the rider move on a map.
- WebSocket hub: You build a WebSocket hub that queues messages per user connection without letting a slow phone block senders.
- Redis event fan-out: You build Redis event fan-out that carries each customer's order events from the worker to the API instance holding their WebSocket.
- Rider management and assignment: You build rider state and assignment so the API can find nearby online riders and give one rider an exclusive claim on a READY order.
- Live rider tracking: You build live rider tracking that streams rider positions to customers and keeps the app connected through a reconnecting WebSocket.
- Live capacity planning: You build a capacity model for rider tracking and customer connections so you can identify the first system limit before load testing.
- Live order extensions: You add replay, topic subscriptions and rider ETA so live order updates can recover after reconnects and customers can follow rider progress.
System architecture and changes
The worker still reads order events from Redpanda. A second consumer group publishes each event to its Redis user channel. Every API instance subscribes to those channels and forwards events to its local WebSocket connections. Redis also holds temporary rider positions, seen keys and order claims. MySQL remains the source of durable order data.
How to complete the coursework
Work through the modules in order: WebSocket hub, Redis event fan-out, rider management and assignment, live rider tracking, live capacity planning, then live order extensions. Start the services with make up. From backend/, run make test W=W5 to run every graded Week 5 test. The end-to-end tests need a running API and TEST_API_URL=http://localhost:8080. In app/, run npx tsc --noEmit and check the rider and customer screens on a phone and an emulator.
Modules
Module 1: WebSocket hub
3 itemsYou build a WebSocket hub that queues messages per user connection without letting a slow phone block senders.
5.1Channels, select and cleanup
You coordinate goroutines with buffered channels and
Open lesson →select, then close channels safely when a connection ends.5.2Managing WebSocket connections
You trace how
Open lesson →coder/websockethandles slow consumers and heartbeats before building a hub that manages each user's connection.5.3Per-user WebSocket hub
You build a per-user WebSocket hub that authenticates
Open exercise →/v1/wsconnections and queues messages for connected users. A full send buffer drops messages without blockingSend.
Module 2: Redis event fan-out
2 itemsYou build Redis event fan-out that carries each customer's order events from the worker to the API instance holding their WebSocket.
5.4Redis pattern subscriptions
You route messages from
Open lesson →user:*to the matching local hub connection so each API instance can receive events published by the worker.5.5Per-user order event bridge
You build a per-user order event bridge that publishes worker events to
Open exercise →user:{id}and forwards them to the customer's local WebSocket. The customer socket must receive the matchingACCEPTEDevent within 15 seconds.
Module 3: Rider management and assignment
3 itemsYou build rider state and assignment so the API can find nearby online riders and give one rider an exclusive claim on a READY order.
5.6Redis GEO and SET NX PX
You can store rider coordinates with Redis GEO, search by distance, and reserve an order with a short-lived claim before implementing rider assignment.
Open lesson →5.7Rider presence service
You build a Redis-backed rider presence service with online, offline and location operations; nearby searches return only active riders in closest-first order.
Open exercise →5.8Rider assignment worker
You build the rider assignment worker to offer READY orders to nearby riders and accept an order only for its current claim holder. A 30-second claim ensures that only one rider can win an order.
Open exercise →
Module 4: Live rider tracking
5 itemsYou build live rider tracking that streams rider positions to customers and keeps the app connected through a reconnecting WebSocket.
5.9Watching foreground location
You can request foreground location permission and render watched coordinates as a map marker, providing the position data the rider screen needs to report.
Open lesson →5.10Reconnecting WebSocket hooks
You can reconnect a mobile WebSocket with backoff and clean up its handlers so the customer map keeps receiving updates after network interruptions.
Open lesson →5.11Order location stream
You build the order location stream, accepting rider coordinates through a per-rider 2000 ms Redis limit and publishing them on an order channel. The API forwards accepted payloads to the customer's WebSocket connection while the order is
Open exercise →PICKED_UP.5.12Rider mode and customer tracking screens
You build rider mode and the customer tracking screen, connecting rider offers and location updates to a live order map and status timeline.
Open exercise →5.13Hub broadcast and claim race tests
You build concurrency tests for the WebSocket hub and Redis rider claims, proving that broadcasts arrive in order and exactly one rider wins each order claim.
Open exercise →
Module 5: Live capacity planning
1 itemsYou build a capacity model for rider tracking and customer connections so you can identify the first system limit before load testing.
5.14Capacity estimation
Estimate connection, Redis and network load for a target rider fleet, then take those assumptions into the Week 6 load tests.
Open lesson →
Module 6: Live order extensions
3 itemsYou add replay, topic subscriptions and rider ETA so live order updates can recover after reconnects and customers can follow rider progress.
5.15Per-user Redis replay log
You build a Redis replay log that accepts user-scoped payloads and returns increasing IDs plus retained entries after a supplied ID. Concurrent appends must receive distinct IDs.
Open exercise →5.16Topic subscription registry
You build a Go topic registry that tracks user subscriptions and publishes messages through a send callback.
Open exercise →Publishreturns the number of accepted sends.5.17Rider map geometry library
You build
Open exercise →app/lib/eta.ts, a pure TypeScript library that takes geographic positions and travel-speed inputs and returns distances, ETA estimates, interpolated positions and compass bearings.
Completion checklist
What should be true at the end of the week.
- From
backend/, runmake test W=W5and confirm every graded Week 5 test passes. - With the API running, execute
cd backend && TEST_API_URL=http://localhost:8080 go test ./e2e -run W5B4_TwoRidersand confirm one rider gets200while the other gets409. - From
app/, runnpx tsc --noEmitand confirm there are no TypeScript errors. - On a phone as the rider and an emulator as the customer, go online, send a location update, and confirm the customer sees the marker move.
- Drop the customer's WebSocket connection and confirm the app reconnects without an app restart.
Week requirements
Rules every piece of code this week must follow.
GET /v1/ws?token=<access token>must reject missing or invalid tokens with401before upgrading, and register a valid connection to the user identified by the token.- A new connection for the same user must replace the old connection. Closing or replacing a connection must remove it and release its goroutines. Concurrent sends and disconnects must remain safe.
Hub.Sendmust never block. It must returnfalsewhen the user has no connection or a message is dropped. Broadcasts must reach every connected user in order.- The worker must publish events to
user:{id}. API instances must forward each valid user channel message only to that user's local connection and ignore malformed channel names. - Rider endpoints must reject customer requests with
403. Online riders must be tracked inriders:onlinewith arider:seen:{id}key that expires after 15 seconds.Nearestmust respect the requested radius and count, return closest riders first, and remove riders whose seen key has expired. - An order claim must use
SET NX PXonorder:{id}:claimfor 30 seconds. Only one rider may hold a claim, only the holder may release it, and an expired claim must be available to another rider. The accept endpoint must return200to the claim holder and409to other riders. Location updates must be limited to one every 2 seconds per rider and publish{lat, lng}onorder:{id}:loc. - Replay logs must assign increasing unique IDs, retain only the newest entries, expire their keys, and return only entries newer than the requested ID in order. Topic subscriptions must be race-free, deliver once per subscriber, count only accepted sends, and stop delivery after unsubscribe or user removal.