monospice / laravel-redis-sentinel-drivers

Redis Sentinel integration for Laravel and Lumen.
MIT License
101 stars 48 forks source link

NotSupportedException on PUB/SUB psubscribe #15

Closed alexli0707 closed 6 years ago

alexli0707 commented 6 years ago

Thandks for you project first. After config redis sentinel when using

RedisSentinel::psubscribe(['*'], function ($message) {
...
}

meet this exception

[Predis\NotSupportedException]                                    
  Cannot initialize a PUB/SUB consumer over aggregate connections.
cyrossignol commented 6 years ago

Update: The package now supports Laravel's native subscribe() and psubscribe() API with version 2.4.0.


Sorry for the delay, @alexli0707. We were on holiday here. This issue is a limitation of the current version of the Predis API. We can use the following lower-level workaround to enable psubscribe():

$client = RedisSentinel::connection('name')->client();
$client->connect();
$client->getConnection()->getCurrent()->psubscribe(...);

Note that we cannot publish() messages to a Redis slave. We need to use a connection to the Redis master:

$client->getConnection()->getMaster()->publish(...);

I'm working on a patch for PUB/SUB and transaction() that makes this more seamless by abstracting these details, but I need to do more testing. The code shown above should work for now.

alexli0707 commented 6 years ago

Thanks, I went to check Predis's API and handle this problem in another way,but I think this way is better~

cyrossignol commented 6 years ago

@alexli0707 - the package now supports Laravel's native subscribe() and psubscribe() API with version 2.4.0. This adds high-availability that we don't get with the previous solution above.