HTTP streaming/push (Comet) with Orbited

Posted on May 06, 2009

I’ve been playing around with HTTP streaming (also known as Comet) for a project I’m working on. I found that the best and easiest solution for me was to use the solution provided by Orbited, an open source project. While Orbited is a great piece software, it took me quite some time to really grasp the architecture even though I read the docs and some articles about it.

I found implementing Orbited is rather easy when you understand the actual architecture. As most articles I could find on this topic are very implementation oriented and not dealing with the overall architecture, I wrote this article focusing on the architecture rather than the actual implementation.

The overall architecture looks like shown on the chart on the right. I will explain each component in detail below.

Browser

The browser connects to Orbited through a javascript library provided by Orbited. This library uses XmlHttpRequest to fake a live streaming connection. The library (along with some server options) allows you to write something like this:


  var client = new STOMPClient();
  client.connect(ip, port);
  client.onmessageframe = function(frame) {
    alert('The server said ' + frame.body);
  };

The IP and port should point at your message queue server (this component is explained in a bit) as seen from the server running the Orbited daemon. The cool thing is that you with few lines can create a live streaming connection from the browser to your message server server – wow! Orbited also offers an IRC client and a XMPP client. Which one to use depends on what language your message queue server speaks.

Orbited

Orbited is a daemon written in Python that runs on your server. It accepts http requests coming from the browser (send through the Orbited javascript library). Orbited routes these request to a message queue server specified by the client.connect() call in the above shown javascript. When the message queue pushes a message, Orbited sends this message out to the onmessageframe-method shown in the example above.

Message queue

A message queue is a daemon that runs on your server. Clients can connect to it and do two things:

  • Send messages to a channel
  • Subscribe to a channel, thus receiving messages when other client sends message to that channel

There are a lot of good message queue servers out there – e.g. RabbitMQ. By default Orbited starts it’s own message queue which is great for testing/developing – no need to setup a separate message queue. You disable this feature in the Orbited config file. Most message queues can use several protocols. So far, I’ve only worked with Stomp which works great for my needs.

Backend

This is where you send your actual messages. All you need to do is to connect to your message queue server, and send a message to a specific channel. The Orbited setup will then make sure this message goes all the way to the browser. Most languages include clients for a lot of open protocols. For example, Ruby’s stomp client makes pushing a message as simple as this:


  client = Stomp::Client.open "stomp://localhost:61613" 
  client.send "channel_name", "My message" 

This message will go all the way back to the onframebodyreceive-method (see the javascript example above), allowing you to use it for whatever you want.

I hope this article gave you a better understanding of how the Orbited setup works. Oh and by the way, be sure to read up on HTML5’s WebSocket which will allow us to establish a full duplex connection directly from the browser via javascript to any host/port thus removing the Orbited link from the connection chain shown in the chart above.

Comments
  1. VenkatMay 08, 2009 @ 11:39 AM

    hey buddy,

    I found your article to be interesting and easy to grasp. could you please give me a simple streaming code which uses Stomp.

    Thanks & regards, Venkat

  2. RasmusMay 09, 2009 @ 07:36 PM

    Hi Venkat,

    Thanks.

    I’m not sure what you mean by “a simple streaming code”. Please elaborate and I’ll post some examples for you.

    You might be able to find what you’re looking for here though.

  3. JimmyMay 14, 2009 @ 08:40 AM

    Good article. But I still wonder what’s the differences between orbited and erlang implemented comet server?

  4. AndrewMay 16, 2009 @ 07:13 AM

    Orbited has good javascript client library, which works out of the box, and plays nice with the server part. In Erlang case you will have to write your javascript side yourself. Which is a lot of work.