Redis · Chapter 16 of 26

Publish and subscribe, live scores

By the end of this chapter you can push each ball to every app server the moment it happens with PUBLISH, listen with SUBSCRIBE and PSUBSCRIBE from ioredis on a connection of their own, say what fire-and-forget means for a phone that was offline, and know when Redis can tell you that a key expired. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Redis from Node.js
  • 4 tasks
  • Redis
  • In the all labs plan

The first 2 chapters of Redis are free. This one comes with the all labs plan.

Why you need this

Rhea wants the score on every fan's phone within a second of the ball. Phones keep a live connection to one of GullyXI's app servers, and there are thirty app servers on match night. The scorer's ball has to reach all thirty, and each passes it on to its own phones.

The streams of chapter 15 could do it: thirty servers each blocking on XREAD after their last id. But that is thirty readers keeping thirty positions, a stream to trim, and history no one here needs: a server that restarts wants the score now, not every ball it missed. And having every phone ask for the score every two seconds is worse: two lakh phones make a lakh requests a second, almost all answering "no change".

What fits is a loudspeaker: the scorer says the ball once, everyone listening hears it at once, and no one keeps a copy. That is Redis's publish and subscribe, pub/sub for short.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    publish-ball.js: publish a ball

    Export publishBall(redis, matchId, ball). Publish the whole ball as JSON text (JSON.stringify of the object you are given) on the channel match:<matchId>:live, and return Redis's reply: how many subscribers heard it.

    The Check publishes with no one listening (the reply is 0), then with two phones subscribed to match:2001:live (the reply is 2, and both get the same JSON, which parses back into the ball), while a phone on match:2002:live hears nothing. Publishing stores nothing: Redis must have no keys afterwards.

  2. Task 2

    scoreboard.js: a scoreboard that listens

    Export subscribeScores(redis, matchId, onBall). Make a connection of its own with redis.duplicate() (the client you are given must never subscribe), subscribe it to match:<matchId>:live, and call onBall(ball) for every message, with the JSON parsed back into an object.

    Resolve only once the subscription is in place (the Check publishes straight after), to a function stop(). stop() unsubscribes and then closes the connection you made, so Redis has as many connections as before.

    The Check publishes three balls on match:2001:live and one on match:2002:live: onBall gets the three, in order. After stop() no one is subscribed to match:2001:live, and the client you were given still answers PING.

  3. Task 3

    all-matches.js: every match with one pattern

    Export watchAllMatches(redis, onBall). On a duplicate of redis, subscribe with one pattern to every channel match:<any id>:live (the Check counts one pattern and no plain channel subscriptions). For each message call onBall(matchId, ball): the match id as a number, taken from the channel's name (2001 from match:2001:live), and the ball parsed from JSON.

    Resolve once subscribed, to a function stop() that unsubscribes the pattern and closes the connection you made.

    The Check publishes on match:2001:live, match:2001:chat, match:2002:live and match:2028:live: onBall gets 2001, 2002 and 2028, in order, and never the chat message.

  4. Task 4 · interview question

    Interview: live-score.js, a phone that opens mid-match

    Export openScoreCard(redis, matchId, onScore). Subscribe a duplicate of redis to match:<matchId>:live, where each message is a ball as JSON, and read the newest entry of the stream match:<matchId>:balls (chapter 15; its fields are text). Call onScore with a score line, "<total>/<wickets> (<over>)", for the newest ball in the stream first (before openScoreCard resolves), and then for every live ball. A match with no stream yet gives no first line.

    Subscribe first, then read the stream, so a ball bowled in between cannot be missed (the Check watches the order Redis ran them). Resolve to a function stop() that unsubscribes and closes the connection you made.

    The Check puts the first 11 balls of match 2001 in the stream: onScore gets "27/1 (1.5)", then "27/1 (1.6)" when the 12th ball is published. Match 2002 has no balls: nothing, until its first live ball gives "4/0 (0.1)".

    Farhan will ask: for live scores, pub/sub or a stream?

What you will learn

  • PUBLISH: send a message on a channel; the reply counts who heard it.
  • SUBSCRIBE and UNSUBSCRIBE: listen to channels, and stop.
  • A subscriber's connection: why it gets its own, and ioredis's message event.
  • Fire and forget: what a subscriber that was not there misses.
  • PSUBSCRIBE: listen to every channel that matches a pattern.
  • Keyspace notifications: Redis telling you a key expired, in one section.

More in Redis from Node.js

Build "Publish and subscribe, live scores" yourself

GullyXI, a Pune fantasy cricket app, falls over on match night. You make it fast with Redis. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Redis are free. This one comes with the all labs plan.