Skip to main content


In a previous blog I started discussing Xmpp and showed how to set up an Xmpp server and connecting to it via Python. In this blog I will dig deeper and show how to implement a simple echo bot.
The code for this lives on Github:


First, let's wrap the network layer. I've picked the Python 3 asyncio for this task.
Let's start by looking at I've created a class called FirstConnection that inherits from asyncio.Protocol.
class FirstConnection(asyncio.Protocol):
    def __init__(self, host): = host
        self.transport = None

    def connect(self):
        loop = asyncio.get_event_loop()
        handler = loop.create_connection(lambda: self,, 5222)

    def connection_made(self, transport):
        logger.debug("Connection made")
        self.transport = transport

        cmd = "<?xml version='1.0'?><stream:stream to='localhost' " \
              "version='1.0' xmlns='jabber:client' " \

        cmd = "<junk/>"

    def connection_lost(self, exc):
        logger.debug("Connection lost")

    def write(self, data):
        logger.debug("Send: %s", data)

    def data_received(self, data):
        logger.debug("Received %d bytes\n%s", len(data), data.decode())
Let's instantiate this class and connect:
    loop = asyncio.get_event_loop()

    bot = FirstConnection("localhost")
If the connect call fails, make sure you have Prosody running. If it is, you should see output similar to this:
DEBUG:__main__:FirstConnection is starting
DEBUG:asyncio:Using selector: KqueueSelector
DEBUG:__main__:Connection made
DEBUG:__main__:Send: <?xml version='1.0'?><stream:stream to='localhost' version='1.0' xmlns='jabber:client' xmlns:stream=''>
DEBUG:__main__:Send: <junk/>
DEBUG:__main__:Received 605 bytes
<?xml version='1.0'?><stream:stream xmlns:stream='' version='1.0' from='localhost' id='743f47de-6f34-4458-afcc-558f872b949a' xml:lang='en' xmlns='jabber:client'><stream:features><starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>PLAIN</mechanism><mechanism>SCRAM-SHA-1</mechanism><mechanism>DIGEST-MD5</mechanism></mechanisms><auth xmlns=''/></stream:features><stream:error><unsupported-stanza-type xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error></stream:stream>
DEBUG:__main__:Connection lost
This is a small step, but an important one. We're talking to the server and have initiated the handshake. The server sent us back its part of the handshake - then we sent it junk and it slammed the door on us. The important bit is that we're doing this in a more managed fashion than before and this allows us to build something useful on top.

Handling XMPP

Before we can actually log in we need to think about how to process the incoming XMPP commands. XMPP is XML based so it makes sense to use an XML parser, rather than working with the raw text.
Python offers several different options for parsing XML, but keeping in mind that we are not dealing with a full XML document, the xml.sax module seems appropriate.
The connection to the XMPP server is stateful - the server expects an initial handshake at the beginning with the right commands being issued in the right order. To manage this, I use a class named XmppHandler. It holds an instance of an xml.sax parser, on which I set the content handler to an XmppContentHandler.
The XmppContentHandler responds to the incoming parser events and builds a queue of XmppElement objects. The XmppHandler pulls from this queue, allowing it to work on a somewhat higher level, with whole XMPP stanzas.
Data received from the server is sent to the handle_raw_response method of the XmppHandler, which passes it on to the feed method of the parser. Once the parser has been fed, I pull the elements out of the queue, processing them one by one until the queue is empty.

Logging in

The processing of elements depends on the state of the handler. The state machine is not implemented in any formal way - it's simply a state attribute and a series of if statements - crude but effective.
The ConnectBot object in takes us a step further. If you run it, you should see it connecting. Note that you need to add an echobot user on the server before it lets you log in. The password should be echobot as well.
prosodyctl adduser echobot@localhost
Once you have added the user, you should see the bot successfully logging in. The log output from connectbot.pyshows you all the XMPP commands that flow between the bot an the server, and the Prosody logs show you the progress as well.

Echo, echo

Finally we can start doing something useful (by some definition of useful). The EchoBot in adds the handle_xmpp_message method. I monkey-patch the handler so that it calls this method whenever a message stanza is received. This method simply pulls the body text from the XMPP element and sends that back to the sender as a message.
Try this out with some XMPP chat client - I've been using Swift. Start a chat with echobot@localhost and you should see everything you say echoed back.

What next?

The hard part is done now - building on this framework we can start focusing on the bots themselves. More on that later!


Popular posts from this blog

Mnesia queries

I've added search and trim to my expiring records module in Erlang. This started out as an in-memory key/value store, that I then migrated over to using Mnesia and eventually to a replicated Mnesia table. The fetch/1 function is already doing a simple query, with match_object. Result=mnesia:match_object(expiring_records, #record{key=Key, value='_', expires_at='_'}, read) The three parameters there are the name of the table - expiring_records, the matching pattern and the lock type (read lock). The fetch/1 function looks up the key as it was added to the table with store/3. If the key is a tuple, we can also do a partial match: Result=mnesia:match_object(expiring_records, #record{key= {'_', "bongo"}, value='_', expires_at='_'}, read) I've added a search/1 function the module that takes in a matching pattern and returns a list of items where the key matches the pattern. Here's the test for the search/1 function: search_partial_…

Replicated Mnesia

I'm still working on my expiring records module in Erlang (see here and here for my previous posts on this). Previously, I had started using Mnesia, but only a RAM based table. I've now switched it over to a replicated disc based table. That was easy enough, but it took a while to figure out how to do, nonetheless. I had assumed that simply adding ... {disc_copies, [node()]} ... to the arguments to mnesia:create_table would be enough. This resulted in an error: {app_test,init_per_testcase, {{badmatch, {aborted, {bad_type,expiring_records,disc_copies,nonode@nohost}}}, ... After some head-scratching and lots of Googling I realized that I was missing a call to mnesia:create_schema to allow it to create disc based tables. My tests for this module are done with common_test so I set up a per suite initialization function like this: init_per_suite(Config) ->mnesia:create_schema([node()]), mnesia:start(…

Optimizing Wine on OS X

I've been doing some performance analysis of EVE running under Wine on OS X. My main test cases are a series of scenes run with the EVE Probe - our internal benchmarking tool. This is far more convenient than running the full EVE client, as it focuses purely on the graphics performance and does not require any user input.

Wine Staging One thing I tried was to build Wine Staging. On its own, that did not really change anything. Turning on CSMT, on the other hand, made quite a difference, taking the average frame time down by 30% for the test scene I used. While the performance boost was significant there were also significant glitches in the rendering, with parts of the scene flickering in and out. Too bad - it means I can't consider this yet for EVE, but I will monitor the progress of this. OpenGL Profiler Apple has the very useful OpenGL profiler available for download. I tried running one of the simpler scenes under the profiler to capture statistics on the OpenGL calls mad…