Scalable XMPP bots with erlang and exmpp, part III

ProcessOne
· 5 min read
Send by email

In the first articles of this series, we learned how to use the exmpp API and how to use it to write a simple external XMPP component.

In this last installment we will complete our component, making it aware of the presence status of our users.

The source code for this article can be downloaded from web_status_v2.tar.gz.

User subscription revisited

If we want to know and keep track of the presence status of our users, we need to make our bot a participating entity in the presence flow of the XMPP network.
Immediately after a user successfully registers with us, our component should send a request, subscribing to the user’s presence information. If he accepts, his server will inform us of his current presence status and from now on it will broadcast to us any subsequent change. Perfect!

But the opposite is not true. As an external component, our bot resides in a kind of virtual domain (webstatus.testdomain) inside our main ejabberd server (testdomain). It is not a normal client, and ejabberd (or any other XMPP server) won’t manage our roster for us. In fact, ejabberd doesn’t even know that it exists, ejabberd’s only duty is to forward any packet directed to webstatus.testdomain to our component. So, we must manage our subscription state by ourselves in the component. This in practice means that our bot must be capable of handling subscription requests directed to it, and must broadcast its own presence information to users that have previously subscribed with it.

We start by adding a new field to the #user{} record, to keep track of the current subscription state.

We are using a simplified set of possible subscription states, this is enough for the purpose of this example. For a complete reference, see RFC3921.

StateDescription
noneno subscription
towe are subscribed to the user’s presence
fromthe user is subscribed to our presence
bothmutual subscription

And then modify the function that handles registration, to subscribe to the user presence when a new user registers with the bot.

Remember that we were using the same code for both registration and preference changes. That is why we check if we are already subscribed with the user before sending the subscription request.

If the user accepts our subscription request, it must reply with a <presence type=”subscribed”> stanza. If he rejects us, it should send a “unsubscribed” response. We must handle these presence stanzas.

First, add a dispatch on presence stanzas (our first bot only handled IQ stanzas):

Now handle the required cases, if the user accepts:

If the user rejects our subscription request or cancels it, we remove it from our users DB:

transition/2 is a function that drives the underling finite state machine behind the subscription state logic. It takes the current subscription state and a received event as inputs, and gives as output the resulting state. Again, it is not complete (it doesn’t take into account subscription requests made but not yet confirmed, etc), but is complete enough for us:

The rest of the process_presence/3 handles are left as exercise to the reader ;) . The lazy reader might prefer to look at the provided sources.

React to presence changes

Now that our bot is successfully subscribed to the presence of our users, let’s start receiving them! Easy enough, we must considerer two cases:

We didn’t talk much about the state field. It is a list of #user_state{} records because in XMPP a user can have several simultaneous connections to the network, all with the same username but with different resource identifiers. That means that at any given time a user can have many active sessions, each one with a different presence status. We keep information for all of them, and when the presence status on one changes, we update the information for it without changing the rest.

Web page

The last missing piece is just display in the user’s page the information that we have.

Presence information is displayed according to user preference settings:

Bonus track

Every one is talking about XMPP and microblogging. As here we don’t want to be less than anyone, lets add a (fairly naive) way to post messages to our web page via our XMPP client. Nothing more than that, posting messages to your own pages. Hey, what more do you want for approximately 20 additional lines of source code?

First, add a new “posts” data field to our user{} record:

Then, handle <message ..> stanzas. We keep only the last ?MAX_POSTS entries:

And finally, display them on the user’s web page:

Final comments

During this article series, we introduced the exmpp API and showed how you can use it in a real world application. In part II, we have learnt the advantages that component bots have over client bots, and developed the basis of our web status component.

In this final part we showed how to manage presence subscriptions in a component bot, one of the few things that needs to be adapted between client bots (for which the server maintains a roster) and components bots (for which the server does not maintain anything).

There are things to not take too seriously though, our component has some simplifications here and there in sake of brevity. For example, it keeps posts and user’s preferences on the same table than user presence, which may not be the smartest design. Presence information change much often, and it doesn’t even need to be persisted; if the bot crashes it will receive them again when it reconnects.