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 clients receives forever, not only once?
Asked
Active
Viewed 6,922 times
3
-
can you give more info to solve – jeni Jul 13 '11 at 10:01
-
it's client to client, and when the client post, the other recieve, perfect, but when the client post again the other isn't recieving.And when i fallow the proccesses in the console, i saw that when the GET is loaded the client can't recieve more, so the get is being loaded when the client send something at the first time – alebash Jul 13 '11 at 10:03
-
for example like chat process?? am i right? – jeni Jul 13 '11 at 10:07
-
Try posting some code samples of what you are working with. – Sukumar Jul 13 '11 at 10:08
1 Answers
7
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.

Prasanth
- 5,230
- 2
- 29
- 61

amitchhajer
- 12,492
- 6
- 40
- 53
-
2Still helpful after 4 years. The only difference is that pubSub() is now pubSubLoop(). Just remember that in the publisher part of the script $redis is the client instance. That's what amitchhajer's response indicates, but I named my variables a little differently and then got confused and tried to publish to the object returned by $redis->pubSubLoop() because its name suggests that it deals with publishing too. – Rafał G. Oct 04 '16 at 13:29