Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (60 sloc) 4.04 KB


This is a pretty straightforward port of Skates the ruby xmpp bot framework.


As with most frameworks the goal is seperation of concerns. You can think of Babylon as being a bit like Rails or Django for developing XMPP bots or components. A request comes in either from the XML Stream (XMPP) or as a user event, and it routed to controller action pairs. Controllers and actions generate XML that they publish back onto the XML Stream. Everything else is just icing.


A big thanks to the people at Jiva Technology who funded the development of this library. Julien Genestoux from Superfeedr for dealing with all my questions about Skates, and many others.

Building your App

We start with a simple container object

HTH = {};


One of the first things you'll want to do when designing an XMPP Bot or component is to respond to incoming XMPP stanzas. Routes provide you the means to match against stanzas and direct them to appropriate actions.

To specify your routes first create a new Router object for your App.

HTH.Router = new Babylon.Router();

Then declare the routes using the query and to functions.

    bind.query('message[type="chat"]').to(HTH.MessageController, "love");
    bind.query('presence').to(HTH.PresenceController, "presence");

Controllers and Actions

Controllers help construct the context by which you render views. Every incoming request instantiates a new Controller and that instance is passed along to the view.

To create a new Controller we'll inject the Babylon controllers prototype into our own object. The only restriction is that your class must take a stanza argument as an initial value.

MyApp.MessageController = function(stanza) { this.stanza = stanza; }
MyApp.MessageController.prototype = new Babylon.Controller();

now we can add new actions to our controller by assigning new functions to the prototype. = function(){
    this.from = ""; = "";
    this.message = "damn you shakespeare";

This will by default call the love view, if no other view is specified.


Views render XMPP stanzas. As such they get put on the xml stream and sent out to the server once rendered.

To create a view, just attach a new function on to the Babylon.Views object. = function(bind) { $msg({type: "chat", to:, from: bind.from}).c("body").t(bind.message)};

This will construct an xmpp message as follows

<message type="chat", from="juliet@capulet.come", to="">
    <body>damn you shakespeare</body>


In babylon as with rails or django, your models provide a tool for managing state, persistence, and logic functionality.

User Input

User input is handled similar to how incoming stanzas are. To catch ui events you'll add another entry to your routers draw function.

    bind.event('message').to(HTH.Message, "send_message");

Then you can use the raise function on the runner, to push events up into the system.

runner.raise("message", {to: "", body: "Hi Frank"});

This will instantiate the HTH.Message controller with the passed in object, and call the send_message action.


Lastly you'll need to be able to run your app. This includes passing in a config and starting the Runner.

var config = { "jid": "juliet@capulet"
             , "password": "romeo"
             , "host": ""
var runner = new Babylon.Runner(MyApp.Router);;

And your bot should now connect to the running Bosh server and respond to messages.