Typorithm · medium

Pub/Sub

redis-pubsubredispubsubpublishchannel
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Pub/Sub

0%

Patterns

SUBSCRIBE listens on a channel and PSUBSCRIBE on a glob pattern; PUBLISH sends a message to all current subscribers as fire-and-forget with no persistence. UNSUBSCRIBE stops listening, and PUBSUB CHANNELS introspects the active channels.

Redis Pub/Sub is a lightweight messaging layer. A client calls SUBSCRIBE news to listen on a channel, or PSUBSCRIBE news.* to match a glob pattern across many channels at once.

PUBLISH news "hello" sends a message to every client subscribed at that moment. It is fire-and-forget: there is no queue and no persistence, so any message published while no one is subscribed is simply lost.

UNSUBSCRIBE stops a client from receiving further messages, and PUBSUB CHANNELS lets you introspect which channels currently have subscribers. When you need durable, replayable messages, reach for Redis Streams instead.

01

SUBSCRIBE for a channel; PSUBSCRIBE for a glob pattern.

02

PUBLISH is fire-and-forget — no persistence.

03

PUBSUB CHANNELS lists active channels.

SUBSCRIBE news
PSUBSCRIBE news.*
PUBLISH news "hello"
UNSUBSCRIBE news
PUBSUB CHANNELS

Read the implementation

Connect each part of the code to the concept before you type.

1. Listen on a channel

SUBSCRIBE registers the client to receive every message published to the named channel.

SUBSCRIBE news

2. Listen on a pattern

PSUBSCRIBE matches a glob pattern, so news.* catches news.sports, news.tech, and so on.

PSUBSCRIBE news.*

3. Send a message

PUBLISH delivers a message to all current subscribers — fire-and-forget, with no persistence.

PUBLISH news "hello"

4. Stop listening

UNSUBSCRIBE removes the client from the channel so it stops receiving messages.

UNSUBSCRIBE news

5. Introspect channels

PUBSUB CHANNELS lists the channels that currently have at least one subscriber.

PUBSUB CHANNELS

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Pub/Sub: Lesson & Practice · Typorithm | ApplyDjinn