Handling presence in a chat application based on Strophe.js

Is there an existing solution that provides presence processing for a chat application based on Strophe.js?

I have a simple chat application based on Strophe.js. I would like to show only users who are online and dynamically change the list. I was wondering if there is an existing solution (maybe a Strophe plugin) that handles this. If there is no such thing, what is the best / easiest way to implement it?

+8
javascript xmpp strophe
source share
2 answers

Using Strophe, you can simply send IQ to your server, which will request a list of your lists, for example:

iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'}); App.connection.sendIQ(iq, your_roster_callback_function); 

This will ask your server for your registries and return an object containing a list of your lists. Then you can iterate over your registries, for example:

 your_roster_callback_function(iq){ $(iq).find('item').each(function(){ var jid = $(this).attr('jid'); // The jabber_id of your contact // You can probably put them in a unordered list and and use their jids as ids. }); App.connection.addHandler(App.on_presence, null, "presence"); App.connection.send($pres()); } 

Note that I added the on_presence and connection.send($pres()) . The goal is so that you can receive updates from your contacts if ever their presence changes. Then your callback will look like this:

 on_presence(presence){ var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc... var from = $(presence).attr('from'); // the jabber_id of the contact if (presence_type != 'error'){ if (presence_type === 'unavailable'){ // Mark contact as offline }else{ var show = $(presence).find("show").text(); // this is what gives away, dnd, etc. if (show === 'chat' || show === ''){ // Mark contact as online }else{ // etc... } } } return true; } 

See the Strophe.js channel documentation for more information. With sendIQ, you can add additional parameters such as error callbacks, etc.

Hope this helps!

Edit:

Forgive me, I made a mistake. $(presence).attr('type') does not give you if the contact is online or not, but it gives you the presence type. The type of presence is actually a signal that tells you if the contact is unavailable, or you are signed, unsubscribed, etc. To your contact.

In the XMPP Documentation :

2.2.1. Presence Types

The 'type' attribute of the presence stanza is OPTIONAL. The presence of a stanza that does not have a type attribute is used for a server on which the sender is online and available for communication. If enabled, the 'type' attribute indicates a lack of availability, request management of a subscription for the presence of another person, a request for the current presence of another person, or an error associated with a previously presented presence stanza. If enabled, the 'type' attribute MUST have one of the following values:

  • unavailable - Signals that the object is no longer available for communication.
  • subscribe - the sender wants to subscribe to the presence of the recipient.
  • signed - the sender has allowed the recipient to receive his presence.
  • unsubscribe - the sender of the unsubscription from another presence.
  • Unsubscribed. The subscription request has been rejected or the previously provided subscription has been canceled. etc...

This $(presence).find("show") provides the status of your contact. From the docs:

2.2.2.1. Show

The OPTIONAL element contains a non-human character XML data that defines a specific object availability status or a specific resource. The presence line MUST NOT contain more than one element. An element MUST NOT have any attributes. If provided, the value of the XML character data MUST be one of the following (additional accessibility types can be defined through the correctly-named child element of the presence stanza):

  • off - the object or resource is temporarily deleted.
  • chat - An object or resource is actively interested in chat.
  • dnd - the object or resource is busy (dnd = "Do Not Disturb").
  • xa - The entity or resource will last for a long period of time (xa = "eXtended Away").

If the show element is not specified, the object is assumed to be online and accessible.

+23
source share

It is important to note that How Correct is it that Strophe.addHandler reads only the first node from the answer? said if you want to read more than just the first presence of a node, be sure to return true at the end, because: "The handler should return true if it should be called again, and returning false will delete the handler after it returns."

So, the solution I used should look something like this:

 on_presence(presence){ var presence_type = $(presence).attr('type'); // unavailable, subscribed, etc... var from = $(presence).attr('from'); // the jabber_id of the contact if (presence_type != 'error'){ if (presence_type === 'unavailable'){ // Mark contact as offline }else{ var show = $(presence).find("show").text(); // this is what gives away, dnd, etc. if (show === 'chat' || show === ''){ // Mark contact as online }else{ // etc... } } } //RETURN TRUE!!!!!!!!! return true; } 
+4
source share

All Articles