Publish, Subscribe

Publish Publish

Description: Publish the message to the channel

Required Parameters

  • Channel: Channels to publish to
  • Message: Messages

Example

Redis::publish('chan-1', 'hello, world!');  // 发送消息
Redis::publish('chan-2', 'hello, world2!'); // 发送消息

Subscribe to subscribe

Description: Subscribe to the channel.

Required Parameters

  • Channels: Multiple channel names need to be an array
  • Callback: The callback function receives 3 parameters.
    • Redis Redis Example
    • Chan Channel name
    • Message received by MSG (mixed)

Example:

function f($redis, $chan, $msg) {
    switch($chan) {
        case 'chan-1':
            ...
            break;

        case 'chan-2':
            ...
            break;

        case 'chan-2':
            ...
            break;
    }
}
// 订阅 3 个 频道

Subscribe to channels by matching mode pSubscribe

You can subscribe to wildcard channels using the Psubscribe method, which you can use to get all messages on all channels.

Parameters

  • Patterns matching rule array
  • Callback accept the closure of the message callback
    • Redis Redis Example
    • Rules for pattern matching
    • Chan Channel name
    • Message received by MSG (mixed)

Example:

Redis::psubscribe(['*'], function ($redis, $pattern, $chan, $msg) {
    echo "Pattern: $pattern\n";
    echo "Channel: $chan\n";
    echo "Payload: $msg\n";
});

Redis::psubscribe(['users.*'], function ($redis, $pattern, $chan, $msg) {
   echo "Pattern: $pattern\n";
   echo "Channel: $chan\n";
   echo "Payload: $msg\n";
});
/docs/2.x/en/redis/pub-sub.html
progress-bar