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

  • Publish sends the supplied payload to user:{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-number and user: channel names. Publishing these malformed names across 10 iterations, with a 20 ms delay between iterations, must not stop the subscription. After cancellation, Subscribe returns within 2 seconds with either nil or an error matching context.Canceled.
  • When a restaurant accepts an order, the ACCEPT transition returns HTTP 200. The customer's WebSocket receives an event whose order_id matches that order and whose to field is ACCEPTED within 15 seconds.

Constraints

  • Keep both exported functions in package realtime, with these exact signatures:
go
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 the user:* 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:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B2

Then run the end-to-end test with the API and worker running:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B2

Read 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 MONITOR while accepting an order as the restaurant. Confirm Redis shows PUBLISH user:{customer_id}.
  • Open the orders screen and confirm it no longer sends a request every 10 seconds.
Finished Exercise 5.5?

Redis event fan-out

2 items

  • 5.4Redis pattern subscriptionsnot completed
  • 5.5Per-user order event bridgenot completed

Rider management and assignment

Up next · Module 3 · 3 items · about 3 h 45 min

Start module 3

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Publish sends payloads to the correct user:{id} channel.
  2. Subscribe forwards messages only to the matching local connection.
  3. Malformed channel names are ignored and cancellation closes the subscription.
  4. The customer's socket receives the matching ACCEPTED event after restaurant acceptance.
  5. Redis shows the expected PUBLISH command and the orders screen no longer polls every 10 seconds.

Commands

terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B2
terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 TEST_API_URL=http://localhost:8080 make test W=5B2

Tests

  • TestW5B2_PublishedMessageReachesOnlyThatUsersConnection in backend/internal/realtime/pubsub_test.go
  • TestW5B2_MalformedChannelNamesAreIgnored in backend/internal/realtime/pubsub_test.go
  • TestW5B2_PublishUsesTheUserChannel in backend/internal/realtime/pubsub_test.go
  • TestW5B2_CustomerSocketReceivesTheAcceptEvent in backend/e2e/w5_test.go