views:

89

answers:

2

I'm using the xmpppy library to write an XMPP client that can chat with users. It has its own XMPP user account and needs to know if a given user is online. However, the documentation is a bit sparse on how to do this. What would you recommend?

The only solution I've seen thus far is to start up a daemon before the XMPP server starts and monitor all presence messages that are sent out - then a user is said to be online if they've sent the "I'm online"-type message but not the corresponding "I'm logging off" message. However, being new to XMPP in general, I would think there would be a nicer way to do this.

+2  A: 

The simple way is to support "subscribe" presence message -- this lets another user check if you're currently present (if they don't already know) by a "subscribe" attempt. Check this useful guide to get started, and the standard for many more important details (esp. on protecting your privacy, if needed, from subscribe requests from user you don't know).

Alex Martelli
Great, excellent links. Thanks for the help!
Chris Bunch
@Chris, you're welcome!
Alex Martelli
+1  A: 

There are basically three ways to connect to an XMPP server: as a client (which you've done), as a component, and as another server. The server-to-server type (s2s) is just a federated connection, very much like how mail servers exchange email with each other.

Alex described how clients keep track of presence. XMPP requires me to approve that you can receive my presence information and vice versa. For your bot this means for you to keep track of who's online the end users need to accept your presence requests. It also means that you can respond to the user's presence requests and keep them informed about if your bot is up or not.

The last way is as a trusted component, and only works if you're running the server. i.e. if you're trying to do this on the jabber.org server, you're out of luck, because you're not running that server. The upsdie is you can have access to the internals of the XMPP server, like pulling lists of everyone who's online. The downside is your component / bot implementation is going to be different for every server implementation.

A. R. Diederich
Yes, I actually ended up going the latter route - I just poll ejabberd to find out everyone who's online since it was a lot easier than having the client keep track of it and my program polling it. Being new to XMPP in general, I thought that there would be an easier way to do this through the xmpppy library, but that ended up not being the case - great answer though!
Chris Bunch