Redis Pub Sub

Redis Pub/Sub is a messaging mechanism for real-time delivery and broadcasting of messages between clients. A client can subscribe to one or more channels. When another client publishes a message to one of those channels, every client subscribed to it receives the message immediately.

The workflow is simple:

  1. The publisher sends a message to a channel with the PUBLISH command.
  2. Redis finds all clients subscribed to that channel.
  3. Redis forwards the message to those clients.

The key point is that Redis does not store messages; it is only a relay. Once a message is sent, any subscriber that is offline loses it for good. (If you want to persist the message use redis stream)

Basic commands (just 4):

  • SUBSCRIBE channel: subscribe to a channel
  • PUBLISH channel message: publish a message to a channel
  • UNSUBSCRIBE channel: unsubscribe from a channel
  • PSUBSCRIBE pattern: pattern subscription. For example, fulim.* subscribes to every channel that starts with “fulim”.

Subscribe to the corresponding channel:

subscribe channel

Publish a message to a Redis channel:

publish channel message

Golang with redis pub sub

package main

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/redis/go-redis/v9"
)

type CS string

func main() {
		PubSub(context.Background(), client)
}

func publish(ctx context.Context, client *redis.Client, channel string, message any) {
	cmd := client.Publish(ctx, channel, message)
	if cmd.Err() == nil {
		n := cmd.Val() // The number of subscribers
		fmt.Printf("%s published a message to channel %s, which currently has %d subscriber(s)\n", ctx.Value(CS("publisher_name")), channel, n)
	} else {
		fmt.Printf("%s failed to publish a message to channel %s: %v\n", ctx.Value(CS("publisher_name")), channel, cmd.Err())
	}
}

func subscribe(ctx context.Context, client *redis.Client, channels []string) {
	ps := client.Subscribe(ctx, channels...)
	defer ps.Close()

	// Loop forever, reading each message as soon as it arrives on the channel
	for {
		if msg, err := ps.ReceiveMessage(ctx); err != nil {
			// The context being cancelled is the normal way this loop ends
			if ctx.Err() == nil {
				fmt.Println(err)
			}
			break
		} else {
			fmt.Printf("%s received a message from channel %s: %s\n", ctx.Value(CS("subscriber_name")), msg.Channel, msg.Payload)
		}
	}
}

func PubSub(ctx context.Context, client *redis.Client) {
	// Cancelling this context stops every subscriber goroutine before the function returns
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()

	ctx1 := context.WithValue(ctx, CS("publisher_name"), "publisher1")
	ctx2 := context.WithValue(ctx, CS("publisher_name"), "publisher2")
	channel1 := "channel1"
	channel2 := "channel2"

	// Start the first batch of subscribers.
	// A subscriber has to be started first; it cannot receive messages that were sent to the channel before that.
	ctx3 := context.WithValue(ctx, CS("subscriber_name"), "subscriber3")
	ctx4 := context.WithValue(ctx, CS("subscriber_name"), "subscriber4")

	go subscribe(ctx3, client, []string{channel1})
	go subscribe(ctx4, client, []string{channel2})
	time.Sleep(1 * time.Second)

	go publish(ctx1, client, channel1, "The white sun sets behind the mountains")
	go publish(ctx2, client, channel1, "The Yellow River flows into the sea")
	time.Sleep(1 * time.Second)
	fmt.Println(strings.Repeat("-", 50))

	// Start the second batch of subscribers
	ctx5 := context.WithValue(ctx, CS("subscriber_name"), "subscriber5")
	go subscribe(ctx5, client, []string{channel1, channel2})
	time.Sleep(1 * time.Second)

	go publish(ctx1, client, channel2, "To see a thousand miles further")
	go publish(ctx2, client, channel2, "Climb one more storey of the tower")
	time.Sleep(1 * time.Second)
}

Output:

publisher1 published a message to channel channel1, which currently has 1 subscriber(s) subscriber3 received a message from channel channel1: The white sun sets behind the mountains subscriber3 received a message from channel channel1: The Yellow River flows into the sea publisher2 published a message to channel channel1, which currently has 1 subscriber(s) -------------------------------------------------- publisher2 published a message to channel channel2, which currently has 2 subscriber(s) publisher1 published a message to channel channel2, which currently has 2 subscriber(s) subscriber5 received a message from channel channel2: To see a thousand miles further subscriber5 received a message from channel channel2: Climb one more storey of the tower subscriber4 received a message from channel channel2: To see a thousand miles further subscriber4 received a message from channel channel2: Climb one more storey of the tower