Closed abbood closed 2 years ago
Did you find any solution @abbood ?
Related to https://github.com/nrk/predis/issues/293
@guyromb i'm afraid i haven't. Well actually I did but as a hack, basically i'm gonna create two distinct redis connections in my laravel app, clustered for caching/queing and non-clustered for pub/sub, see details here: https://stackoverflow.com/a/57752477/766570 (vote up if you like :)
Hi @abbood,
I don't know how Laravel works so I will just give an example by using a plain Predis\Client
instance connected to redis-cluster:
$pubsubClient = $client->getClientFor("$ip:$port");
$pubsubClient->publish($channel, $message);
The first line returns a new client instance targeting a single node by using its $ip:$port
pair (which is the ID of a node in the cluster), that client can then be used for Pub/Sub (either by using Redis commands directly or by using the higher-level abstraction returned by Predis\Client::pubSubLoop()
).
If you don't have an $ip:$port
pair and you are just interested in a random node, the snipped is a bit more convoluted:
$randomSlot = mt_rand(0, 16383); // Random slot in the range used by redis-cluster
$nodeConnection = $client->getConnection()->getConnectionBySlot($randomSlot);
$pubsubClient = new Predis\Client($nodeConnection, $client->getOptions());
$pubsubClient->publish($channel, $message);
Admittedly this is not an ideal approach but we already have some improvements in this regard planned (and in part already commited) for Predis v2.0.
As for Laravel, like I said I don't know its internals so I'm probably going to say something stupid but since they use a facade anyway maybe they could implement concrete methods for PUBLISH
, SUBSCRIBE
, UNSUBSCRIBE
and related Pub/Sub commands in a way so that when connected to redis-cluster these commands can be transparently relayed to a single node using a random slot generated like in the above snippet. The catch is that once a slot is picked it's better to stick to the same slot for subsequent Pub/Sub commands (caching the slot ID internally should be fine). This is just a rough idea, @tillkruss is the Laravel expert here and should know if such an approach would be feasible or not.
I got a redis cluster server which I created by following this tutorial.
This is what my database/config.php setup looks like (see details here):
and what my config/broadcasting.php looks like:
when i reach this part in my code when i want to publish an event in redis:
I get this error:
with this stack trace
how do i fix this?