Replies: 2 comments 8 replies
-
|
@michael-grunder Any thoughts? |
Beta Was this translation helpful? Give feedback.
-
|
Hi @roimee6 You can unsubscribe inside the callback. Here's an example program that will do just that: <?php
$relay = new \Relay\Relay('localhost', 6379);
$relay->subscribe(['foo'], function ($relay, $channel, $message) {
if ($message == 'unsubscribe') {
printf("Unsubscribing from %s\n", $channel);
$relay->unsubscribe([$channel]);
} else {
printf("%s => %s\n", $channel, $message);
}
});
printf("Finished with subscribe loop, exiting\n");You can verify this by publishing This also works if you're subscribed to multiple channels: <?php
$relay = new \Relay\Relay('localhost', 6379);
$relay->subscribe(['one', 'two', 'three'], function ($relay, $channel, $message) {
if ($message == 'unsubscribe') {
printf("Unsubscribing from %s\n", $channel);
$relay->unsubscribe([$channel]);
} else if ($message == 'unsubscribe-all') {
printf("Unsubscribing from all *\n");
$relay->unsubscribe([]);
} else {
printf("%s => %s\n", $channel, $message);
}
});
printf("Finished with subscribe loop, exiting\n");Relay can do other cool things such as being able to subscribe to a channel within the subscribe callback without recursing the call-stack, meaning you could in theory subscribe to a "control" channel and then subscribe and unsubscribe to other channels based on the messages. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now when I do that:
I can't unsuscribe directly because it's an infinite loop I guess
So I propose as a likely function:
The function name is an example
I think you can find another way to do what I want, but I think it would be a good idea to do something like this
Thanks
Beta Was this translation helpful? Give feedback.
All reactions