Skip to content

How to receive messages from Pub/Sub Subscription #131

@raultruco

Description

@raultruco

Hello, I'm not sure if this feature is implemented in Yii2-redis extension....
but I wanted to work with Pub/Sub using this extension, specifically receive messages of a subscribed key in Redis. Publish part is working fine instead.

I have tried several ways to receive the messages from other publishers:

  1. Passing as the latest parameter an array formed by [, ] or a string with the function name. This is how native PHP redis driver works. But I get an error saying: 'mb_strlen() expects parameter 1 to be string, object given'
$subscriptionResponse = Yii::$app->redisSubscriber->subscribe(
    'sync',
    [$this, '_handleSyncInMessage']
);

or

$subscriptionResponse = Yii::$app->redisSubscriber->subscribe(
    'sync',
    '_handleSyncInMessage'
);
  1. Putting the callback function inline doesn't work neither. The same error: 'mb_strlen() expects parameter 1 to be string, object given'
$subscriptionResponse = Yii::$app->redisSubscriber->subscribe(
    'sync',
    function($redis, $channel, $message) {
      echo 'Never arrived here..."
    }
);

The following code doesn't throw any error, but I don't know how to receive the messages when other processes publish in the key 'sync':

Yii::$app->redisSubscriber->subscribe('sync');

Anyone has a working example of this?

I also put this question in yiiframework forums here

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions