ralphm.net

ralphm's blog

Saturday, 8 October 2005

Twisted Virtual Sprint

Hacking away...

After a number of succesful Australian sprints, this weekend people gather for the first, experimental, Twisted Virtual Sprint. Anyone interested in helping out improving the code and/or documentation is welcome to participate by joining the #twisted IRC channel on irc.freenode.org.

As maintainer of the Streaming XML bits in Twisted (including Jabber support), I've committed to work on removing the last Jabber-specific stuff in Xish' xmlstream module, making it generically usable for any kind of streaming XML. The code that is taken out will be moved to Twisted Words. The plan is to make the Jabber support in Words XMPP compliant. For example, it should support stream features like SASL and TLS, gain an idea of the three XML Stanzas and also properly handle stream level and stanza level errors. I've started on this a while back already, putting my code in my personal branch in Twisted's SVN repository.

So, what do you use a generic Streaming XML module for. Streaming XML is basically opening a socket and exchanging first-level elements (direct childs of the root element that we call stanzas) during the lifetime of the connection. An example use is handling FeedMesh streams. The concept of FeedMesh is sharing pings of updated blogs between entities like PubSub.com and blo.gs. Monitoring this stream can be as simple as the following snippet:

from twisted.xish import xmlstream
from twisted.internet import reactor

def onPing(element):
    print "Ping for %s" % (element["rss"])

def connected(xs):
    print "Connected!"
    xs.addObserver('/weblog', onPing)

f = xmlstream.XmlStreamFactory()
f.addBootstrap(xmlstream.STREAM_START_EVENT, connected)

reactor.connectTCP('sandbox.pubsub.com', 9999, f)
reactor.run()

This opens a connection to PubSub.com's experimental service. When stream has been started (the client will receive a root element), the STREAM_START_EVENT event is dispatched. The function connected will be called with the actual XmlStream object. This function sets up a new handler based on an XPath-like expression. In this case, we want to monitor weblog stanzas. The onPing function will be called for each matching element, passing the object representing the element. The element's attributes are available as items of the object.


Note:

The FeedMesh services have varying terms of service. Currently, they are at most experimental and you should not use them for updating feeds in a (desktop) feed reader, unless the service operator specifically allows this.