Pub/Sub Messaging with Redis

Pub/Sub Messaging with PHP and Redis

Welcome back! In this lesson, we will dive into another powerful feature of Redis: publish/subscribe (pub/sub) messaging. This topic builds on our understanding of Redis and introduces a dynamic way to enable real-time communication within your applications.You will learn how to set up and use Redis pub/sub messaging to send and receive messages between different parts of your application. This is useful for creating real-time features like notifications, chat systems, or live updates.

Let's look at how you can set up a simple pub/sub system in the code snippet below:

<?php
require 'vendor/autoload.php';

use Predis\Client;

// values to use in the program
$messageLimit = 10;
$messagePrefix = "my-message-";
$channel = "my-channel";

// two clients, one as the publisher, the other as the subscriber
$publisher = new Client();
$subscriber = (new Client())->pubSubLoop();

// subscribing to the relevant channel
$subscriber->subscribe($channel);

// publishing 10 messages to the relevant channel
for ($i = 1; $i <= $messageLimit; $i++) {
    $publisher->publish($channel, $messagePrefix.$i);
}

// loop reading incoming messages
foreach($subscriber as $message) {
    echo "Message received in channel [$message->channel]:\n";
    echo "    [ KIND    => $message->kind ]\n";
    echo "    [ PAYLOAD => $message->payload ]\n\n";
    // the check here ensures we break the loop when we reach the last message
    // otherwise the program will wait for the next message indefinitely
    if ($message->payload == $messagePrefix.$messageLimit) {
        break;
    }
}

?>

The code demonstrates a simple pub/sub system, where a publisher sends messages to a channel and a subscriber listens and processes these messages in real-time. Let's break it down:

  • We start by requiring the vendor/autoload.php to load the Predis library.
  • We define variables for the message limit, message prefix, and channel name.
  • We create two Predis\Client objects: one for publishing messages and another for subscribing to the channel.
  • The subscriber client subscribes to the specified channel using the subscribe method.
  • We publish a series of messages to the channel using a loop, appending a number to the message prefix.
  • The subscriber enters a loop to listen for incoming messages on the channel.
  • For each message received, we print the channel name, message kind, and payload.
  • A condition checks if the last message has been received to break the loop and prevent indefinite waiting.

The responses ready in this particular example would look like this:

text
Message received in channel [my-channel]:
    [ KIND    => subscribe ]
    [ PAYLOAD => 1 ]

Message received in channel [my-channel]:
    [ KIND    => message ]
    [ PAYLOAD => my-message-1 ]

// ...multiple messages...

Message received in channel [my-channel]:
    [ KIND    => message ]
    [ PAYLOAD => my-message-10 ]
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal