Per-user order event bridge
You build a per-user order event bridge that publishes worker events to user:{id} and forwards them to the customer's local WebSocket. The customer socket must receive the matching ACCEPTED event within 15 seconds.
Ticket
Build the per-user realtime event bridge. A worker consumer reads order-events in its own Kafka consumer group named realtime and publishes each order event to user:{customer_id}. Every API process subscribes to user:* and forwards valid messages to its local WebSocket hub.
The worker does not know which API instance owns a customer's connection. Redis pub/sub carries the event across that boundary. Replace the orders screen's 10-second polling with socket-based updates. The same live update path supports the customer-facing features built later in Week 5.
Acceptance criteria
Publishsends the supplied payload touser:{decimal_user_id}. For the payload{"to":"READY"}, a subscriber on that user channel receives exactly{"to":"READY"}within the test's 5-second context.- With two connected users, an active subscription forwards
{"order_id":42,"to":"ACCEPTED"}to the intended user's connection only. Both connections register within 2 seconds, the target receives the message within 5 seconds, and the other connection receives nothing during a 300 ms quiet read. - The subscription ignores
user:not-a-numberanduser:channel names. Publishing these malformed names across 10 iterations, with a 20 ms delay between iterations, must not stop the subscription. After cancellation,Subscribereturns within 2 seconds with eithernilor an error matchingcontext.Canceled. - When a restaurant accepts an order, the
ACCEPTtransition returns HTTP200. The customer's WebSocket receives an event whoseorder_idmatches that order and whosetofield isACCEPTEDwithin 15 seconds.
Constraints
- Keep both exported functions in package
realtime, with these exact signatures:
func Publish(ctx context.Context, rdb *redis.Client, userID int64, payload []byte) error
func Subscribe(ctx context.Context, rdb *redis.Client, hub *ws.Hub) error- Use the
user:{decimal_user_id}Redis channel for publishing and theuser:*pattern for subscribing. - Ignore channel names whose suffix cannot be parsed as an integer.
- Tie the Redis subscription to the supplied context. Close the subscription when the function returns.
Design direction
Keep Redis transport in backend/internal/realtime. The worker should own Kafka consumption and customer identification, then call realtime.Publish. Each API process should own one process-level subscription that forwards messages to its local hub. Keep the Redis subscription independent of individual WebSocket connections.
In backend/cmd/worker/main.go, configure the second Kafka consumer group as realtime on the existing order-events topic. Keep its offset handling independent of the notifier consumer group.
In backend/cmd/api/main.go, start one realtime.Subscribe(ctx, rdb, hub) goroutine tied to the API process context. Avoid creating a subscription for every connection. In the app, remove the 10-second polling from app/app/orders/[id].tsx and use the socket update path.
Keep the transport simple. Redis pub/sub routes messages between processes, but it does not retain them for disconnected customers. MySQL remains the source of truth, and the app can refetch order state after reconnecting. Any package layout that meets the contract passes.
Hints
How to check your work
Run the package tests first:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B2Then run the end-to-end test with the API and worker running:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B2Read the first failing assertion before changing anything. It identifies whether the failure is in channel routing, malformed-message handling, subscription cancellation or the full event path.
Also verify the two behaviours not covered by the unit tests:
- Run
redis-cli MONITORwhile accepting an order as the restaurant. Confirm Redis showsPUBLISH user:{customer_id}. - Open the orders screen and confirm it no longer sends a request every 10 seconds.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Redis event fan-out
2 items
- 5.4Redis pattern subscriptionsnot completed
- 5.5Per-user order event bridgenot completed
Up next · Module 3 · 3 items · about 3 h 45 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
Publishsends payloads to the correctuser:{id}channel.Subscribeforwards messages only to the matching local connection.- Malformed channel names are ignored and cancellation closes the subscription.
- The customer's socket receives the matching
ACCEPTEDevent after restaurant acceptance. - Redis shows the expected
PUBLISHcommand and the orders screen no longer polls every 10 seconds.
Commands
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B2cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B2Tests
TestW5B2_PublishedMessageReachesOnlyThatUsersConnectioninbackend/internal/realtime/pubsub_test.goTestW5B2_MalformedChannelNamesAreIgnoredinbackend/internal/realtime/pubsub_test.goTestW5B2_PublishUsesTheUserChannelinbackend/internal/realtime/pubsub_test.goTestW5B2_CustomerSocketReceivesTheAcceptEventinbackend/e2e/w5_test.go