Publish, subscribe

Publish

Description: Post a message to the channel

Required parameters

  • Channel: the channel to publish to
  • Message: message

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 instance
    • Chan channel name
    • Msg received message (mixed)

example:

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

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

        case 'chan-2':
            ...
            break;
    }
}
// 订阅 3 个 频道
Redis::subscribe(['chan-1', 'chan-2', 'chan-3'], 'f');  

Subscribe to channel pSubscribe by matching mode

Use the psubscribe method to subscribe to wildcard channels, which can be used to get all messages on all channels.

parameter

  • Patterns matching rule array
  • Callback accepts the closure of the message callback
    • Redis redis instance
    • Pattern matching rules
    • Chan channel name
    • Msg received message (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