How to user predis for publish more than one time
How can I publish info between the clients more than once?
I mean when I publish info from one user to other, he receives and backwards, but this is only once. Because when one user send something to the other, the GET is being loaded and the receiving stops, how can I make it that way so the 开发者_如何学Goclients receives forever, not only once?How pub/sub works: like a channel, you put from one side and you get the same from the other side.
So publisher data will be received only when there is some subscriber for it.
Use pubSub context and subscribe to a channel say "x" and from another side, keep taking the data, say from User, and publish it using publish command every time to the same channel.
Subscriber:
$redis = new Predis\Client(// put setting here, if req);
$pubsub = $redis->pubSub();
$pubsub->subscribe($channel1);
foreach ($pubsub as $message)
{
switch ($message->kind) {
case 'subscribe':
echo "Subscribed to {$message->channel}\n";
break;
case 'message':
// do something
break;
}
}
Publisher:
while(1) // or whatever condition
{
$redis->publish($channel2, $userdata);
}
You can use chat messages to break the connection, e.g. publish exit and check at subscriber if exit then close the connection and then check at publisher side if no subscriber attached, close it too.
精彩评论