Twisted and XMPP

2007-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 user@gmail.com 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.

Posted in jabber python

3 Responses to "Twisted and XMPP"

  1. Didier Rano Says:

    Hi, Thank you for your knowledge about Twisted and XMPP. I am very impressive by it. In which domain, are you working to have such important knowledge ? In fact, I try xmpp.client.py as you proposed, but I have an error "twisted.words.protocols.jabber.sasl.SASLNoAcceptableMechanism" in init_failed method. I used the twisted trunk (Revision 23060). Have you some ideas about this problem ?

    1. Jason Huggins says:

      I'm getting the same error as Didier with the xmpp_client.py example (Twisted-8.1.0, Python 2.4, on Mac OS X 10.4 Tiger). I'm still waiting to start loving Twisted. :-)

      1. Raizyr says:

        Chances are that you don't have pyOpenSSL installed. That was the problem for me with the same error message.

        Leave a Reply

        Not logged in. You have to fill the data below to leave a post. Please register here

        Nickname:

        Email:

        Antispam code:

        XHTML: You can use these tags: [ a abbr acronym b br blockquote code pre em i strike strong ]


        To send a trackback, use this url.