Twisted and XMPP2007-05-26 12:21:40 by Fabio Forno
Time ago I started writing a tutorial for the XMPP support in Twisted, but for technical problems I had to suspend it. I will resume it as soon as I can.
The problem, I noticed from several requests I've received recently, is that this library is not very friendly for beginners. Twisted is not the easiest framework to learn, since you need to modify many of you programming habits; the word.protocols.jabber package has gone under heavy development and there is a bit of confusion about which version to use for which XMPP server. And how can I do with Google Talk? Is it possible to connect to? Are there any examples?
First few words about XMPP and Google Talk. Historically XMPP, when the protocol was named Jabber, had a different authentication method, based on <iq/> messages in the "jabber:iq:auth" namespace. Now that method is deprecated and maintained by few servers for legacy reasons, the only supported authentication method is SASL, which is well described in RFC 3920. SASL indeed is a general framework for plugging in different authentication mechanisms, and among them the most used in XMPP are PLAIN and MD5-DIGEST. Google Talk is 100% XMPP and it supports two authentication mechanisms: PLAIN and GOOGLE-TOKEN, no digest since Google Talk allows TLS connections only. With Google Talk you must only pay attention to one detail: differently from many XMPP servers the realm you are authenticating to (gmail.com) is different from the physical server you are connecting to. Which is the server? It's talk.google.com, port 5222, but you can't rely on this. There is standard way to discover the host, and you should use it: clients should query the "xmpp-client" SRV record of the desired domain (e.g. gmail.com).
So, getting back to Twisted, where do I start from? Though many Linux distributions still come with Twisted 2.4 your choice is Twisted 2.5, which is the only one implementing SASL and being able to connect to standard compliant servers, such as Google Talk. This version has an example that shows one possible way to write a client: xmpp_client.py, in the words package examples. This example uses a SRV dns query in order to discover the host to connect to, so it can connect to gmail. So just download Twisted 2.5, install it and the try the xmpp_client.py with this command line:
python xmpp_client.py firstname.lastname@example.org yourpasswword
If everything works well, you should see a dump of the exchanged packets with server.
As you can see, picking the right version and the right example, the startup time for using and understanding Twisted is almost as low as for the other XMPP libraries. And once you have become familiar, I think you'll start loving Twisted as I do.