tags:

views:

33

answers:

1

I am coding a xmpp component using http://xmppjs.prosody.im/. I want to sent message to a jid, but only if user is online. What is the quickest/most efficient way to achieve this.

I would like to detect this so that for example I could send an e-mail when the user is offline.

+2  A: 

Send the message, with type='headline', regardless of whether the JID is online. Headline messages should not get stored for later delivery if the recipient is offline.

This approach is much quicker and more portable than anything else you can do.

If you MUST do something different based on whether the user is online, you'll need to subscribe to that user's presence:

<presence type='subscribe' from='my.component' to='user@domain'/>

The user will have to accept your request:

<presence type='subscribed' to='my.component' from='user@domain'/>

The user may also subscribe back to your component:

<presence type='subscribe' to='my.component' from='user@domain'/>

You can do what you choose with those; the easiest thing to do is to just accept them:

<presence type='subscribe' from='my.component' to='user@domain'/>

Every time you need their presence, or every time your component starts up, it will need to send a probe to that user:

<presence type='probe' from='my.component' to='user@domain'/>

You will then get back the presence of each of the user's resources. There is no way to tell when you're done. If you want to take action when the user is offline, you just have to choose a timeout value, then stop the timer if you receive a presence. Since the user is offline anyway, the extra latency usually isn't a big problem.

Note that if you accepted the user's presence subscription above, you'll get probes from that user; you can either ignore them, or respond with a presence stanza that describes your operational state.

Joe Hildebrand
Still very good answer :). I am going to rewrite my question and if not that answer i will accept yours definitily.
Alfred
thanks for the update :)
Alfred