Writing XIFF Extensions To Pass Custom Data Over XMPP
As I mentioned in an earlier post, I was working on building a chat room using XMPP (Jabber) and the XIFF AS3 API.
If you aren’t sure how to get started with XMPP and XIFF, make sure to check out that post to get you up and running.
Once I had successfully worked out using XIFF to connect with XMPP and pass data back and forth between the server and the client, I soon realized that I wanted to associate custom data with each user in the chat rooms.
The core protocol used in XMPP to express a user’s current network availability (offline or online, etc) is Presence.
On IM, you know when your buddies come online and become unavailable, etc because you subscribe to their presence.
The user’s presence is also sent to a chat room when he/she joins the room.
The default presence data typically only incorporates information about the user’s status (away, busy, available, etc).
The issue that I soon ran into with my chat rooms, was that our site already had a large collection of users with data associated with each user (id, username, picture, etc) and I wanted to associate this data with the chat users without duplicating the data.
The best place seemed to be their presence since its already linked directly with each user. The only issue I had was figuring out how to do that.
As I soon realized, the XMPP spec is extremely extensible and well thought through and XIFF has tried to manage this extensibility in its library as well.
In XIFF, extensions can be added to any
XMPPStanza that is sent to the XMPP server.
By default, there are three core XMPP stanzas,
Each of these stanza classes has an
What I needed to do was write a custom extension class that I could pass into the Presence’s
Digging in to the code, all XIFF extensions need to extend the
Extension class and implement
IExtension, you will need to add
getNS() will return the custom unique namespace that you will need to create and associate with this extension.
getElementName() will return the base XML element name to associate with this extension.
ISerializable, you will need to add
serialize() will convert the typed extension into XML data to be passed to the server while
deserialize() will do the opposite and convert the received XML data into the typed extension.
For more detailed information on this, make sure to look at some of the existing extensions in XIFF’s library.
Once the class is complete, you will need to register the extension with XIFF. This registration process informs XIFF of the particular extension so that it knows how to handle it when it is received over the network.
This can be done by calling
ExtensionClassRegistry.register( CustomExtension ) anytime before you try to pass the extension over XMPP.
Most extensions provide a static
enable() method with the above code in it to handle this registration process.
At this point, you can create an instance of any of the core XMPP stanzas (IQ, Message, or Presence) and an instance of your new extension and then use the
addExtension() method of the core stanza to pass in your extension.
The last step is to call the
send() method on your XMPP connection passing in the XMPP stanza.
To finish up my particular example, I created a custom user extension that had the user’s id, name, picture, etc and attached it to the user’s presence.
This as mentioned above could be passed with the send method on the XMPP connection and anyone subscribed to my user’s presence would receive the custom extension as well.
One thing that I wanted to do was join a chat room and have this custom data associated with the presence information of all of the existing users in the room that gets broadcast out.
It took me a little bit to realize that the Room class has a feature for this already. Like I said, XMPP has thought of everything already.
When you join a chat room, you call the Room’s
join() method and the second parameter allows you to add an array of Presence extensions. Just instantiate your custom Presence extension and wrap it in an array and pass it into the
Hopefully this helps out others that got stuck on sending custom data over XMPP with XIFF.
Let me know if you have any questions or want any other XIFF/XMPP tutorials.