Next Developer
Next Developer

Reputation: 1229

How to know if an user is offline already by the time I send him/her a message?

I am building a chat based on Comet technology and I am stuck with this problem. So, for comet, i am using ningx with it's push module. Everything works fine. I can send messages to another user via dedicated channels. However, the problem appeared when I started thinking what will happen to the message which was sent to a user who went offline before receiving that message. I'd like before pushing the message into a channel to check if the receiver is still listening that channel. If so, I will just push the message otherwise I want just put the message into the database so that the user could read it when he/she is online.

QUESTION:

  1. Can I check somehow if a specific channel currently has at least one subscriber (for nginx push module)? if it's impossible then how to know for sure that the user to who the message is sent is online? (last activity is not going to help, cuz it's impossible to be sure that the user did not go offline one second before he/she was supposed to receive the message)

Thanks!

Upvotes: 0

Views: 223

Answers (1)

Next Developer
Next Developer

Reputation: 1229

I've answered on my own question. Here's the code I use to publish a message and how I get an useful information back after publishing it.

$channel_id = 'c'.$_POST['uid'];

$message['sender']=$_SESSION['user.ID'];
$message['firstname']=$_SESSION['user.firstname'];
$message['message']=$_POST['msg'];
$message['type']='chat';

$c = curl_init('http://192.168.56.101/publish?cid='.$channel_id);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'pre('.json_encode($message).')');
$r = curl_exec($c);

in this example var $r contains this: queued messages: 0 last requested: 4 sec. ago (-1=never) active subscribers: 1

as u can see, it also contains a number of subscribers

Upvotes: 1

Related Questions