Orbited

Comet Server Ingredients

by Frank SalimJune 10th, 2008

It seems like everybody and their brother’s goldfish is writing a Comet server these days. The list of implementations has grown dizzyingly long, without any clear leaders emerging. Most of these are simple long-polling servers. Many have no client-side code and instead ask developers to provide their own JavaScript. One, pi.comet, does the opposite. It supplies the JavaScript and asks developers to provide their own long-polling server. It’s a wild world that we live in.

Just as it is quite easy to write a web server and devilishly hard to write a successful Apache killer, it is trivial to make a simple long-polling server compared to the task of developing a mature and full-featured messaging server. Well, I think everyone should write a web server. It’s a good exercise. The fact that everyone else is working on a Comet server probably shouldn’t deter you from making one, either.

The recipe for getting up and running with your very own Comet server from scratch is quite simple:

  • Asynchronous IO
  • HTTP
  • JavaScript
  • Message Dispatching

Without further delay, let us head to the kitchen and start cooking.

Comet Server Ingredients

Asynchronous Network Server

Using one thread per connection, the longtime standard for web servers, is completely impractical for Comet. The performance cost of idle threads is very high, and Comet relies on keeping many idle connections open. If you have aspirations for more than a double-digit number of users, it is best to bite the asynchronous bullet right away. Twisted has been a very popular choice for asynchronous frameworks. I count no fewer than 6 Comet servers based on Twisted, and I’m sure that number will double in the next year.

HTTP

Comet servers are specialized web servers. As such, many frameworks for writing web servers are not appropriate for use with Comet. Comet requires low-level access to HTTP requests or sockets. For streaming transports, you will need to incrementally write a response. For long-polling, delay the response until there is data to send downstream. Either way, you are either going to have to parse HTTP or use a framework that exposes that level of control. Several of the leading Comet servers have their own HTTP parsing code. Alternatively, you could borrow a well-respected parser from another project.

JavaScript

Your fancy new asynchronous server won’t be of much use without a browser-side client library. Love it or hate it, JavaScript is essential for Comet. This is a web-oriented technology, and the web means JavaScript. Many of the most important advances in this niche have been archaeological — discovering how to use uncommon or undocumented browser behavior in pursuit of better Comet transports.

You will want to conceal the dirty sausage-making details of Comet and provide a nice interface for developers to use. Also, leave yourself room to support the messaging features in HTML 5 without breaking your applications.

Message Dispatching

Now that you have a server that sends messages to web browsers, you need a way to get those messages from your app to the Comet server. Perhaps your applications will be written as plugins and run inside the server process itself. However, the suggested method that will grant the most flexibility to your future app developers is to make your server a standalone process and have a dispatch protocol. Maybe you will use STOMP, the Streaming Text Oriented Messaging Protocol.

Congratulations!

You now have a Comet server. You are well on your well to joining the throng of other Comet server hopefuls. All you need now is a web site and a funny name (probably a play on words relating to astronomy). If you want to pull away from that pack, however, you’re going to need to stay for the bonus round.

Extra Credit

Reliable Messaging

You’ll find that some transports, such as streaming iframe transports, will drop messages that were put on the wire while clients are reconnecting. This is unavoidable. There is nothing the server can do to detect when a connection has silently closed. Instead, employ an acknowledgment scheme by which the server can resend any messages that the client missed. As of this writing, there are no production ready Comet servers that will preserve message ordering and guaranteed delivery in the face of intermittent connectivity or flaky streaming transports. (There is an experimental branch of Orbited that does.) To write a Comet server that really distinguishes itself, you cannot simply lose messages into the void.

Addressing

Any self-respecting Comet server should be able to run multiple applications. It isn’t unreasonable that a site would want to deploy a chat widget, a notification widget, and several other real-time data views without having to make each application aware of the others. You will need some strategy for keeping different comet applications from stepping on their neighbors’ toes. Because of the browser connection limits, simply mapping URLs to applications might not suffice. Instead, you could send every message down on the same stream. Label them with identifiers, similar to ports. The JavaScript library, then, would route these messages to the appropriate widgets.

PubSub

PubSub, short for Publish/Subscribe, is a very natural model for building a large number of applications. It is especially well suited for applications that consist of channels, resources, or locations. Write a PubSub backend that plugs into your comet server and dispatches messages to subscribers. Better yet, integrate with existing distributed PubSub clouds.

Security

There is plenty of room in the world for a Comet server with good security features. It would be great to include the option to serve encrypted messages with SSL. Additionally, you could provide authenticated channels by which the application writer can control which users can publish and subscribe to various resources. Extra super-duper bonus points will be awarded if this ties into existing authentication methods such as LDAP.

Conclusion

That is the basic recipe for most Comet servers. It should now be no surprise how many new implementations are cropping up. For dessert we saw a partial roadmap for an advanced Comet server. If you go so far as to follow that plan, please package your server for Debian and send me a note. :-)

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Lightstreamer

7 Responses to “Comet Server Ingredients”

  1. Michael Carter Says:

    As to the extra credit — wouldn’t it make sense just to bridge into an existing Message Queue so as to avoid the headaches associated with those features?

  2. Martin Tyler Says:

    “As of this writing, there are no production ready Comet servers that will preserve message ordering and guaranteed delivery in the face of intermittent connectivity or flaky streaming transports”

    Have you tried them all? Liberator fits that bill.

    Otherwise, good article :)

  3. Frank Salim Says:

    Michael:
    From the ‘extra credit’ section, only pub/sub gets offloaded by bridging to a message queue. Unreliable transports, multiplexing, and encryption all need to be handled by the Comet server. That is, unless you handle cryptography in JavaScript(!) and used a message queue with reliable messaging. That leaves multiplexing. You could make the case that a message queue would itself provide multiplexing in the form of multiple channels. That might be enough for some people. A similar alternative is to take a message queue and embed a Comet server in it, the way ActiveMQ has done.

    Martin:
    Sorry! No, I have not tried them all. I shouldn’t make blanket statements like that. In my defense, I wrote this article before your “Guaranteed Messaging in Comet” came out, but I definitely should have caught that in revision. Mea culpa.

  4. Alex Kritikos Says:

    Frank, Michael, Martin:

    Nirvana from my-Channels (http://www.my-channels.com) is an enterprise MOM product with Comet support that addresses the features required, preserves message ordering, multiplexes connections. We have not embedded a third party Comet server inside Nirvana, as our server has a built in http stack for years now supporting non ajax clients through firewalls without any additional infrastructure components.

  5. Erika N. Says:

    This is ever-so-slightly off-topic, but I’ve got a question for you comet folks.

    So, armed with knowledge gleaned from a few good articles on comet, I put together a little request handler that lets me do long polling and keep track of an event queue. It works great for two or three users for chat or a simple game, and it’s fun to play around with. However, with at least one thread open per user at a time, it’s not going to scale at all.

    I don’t actually want to write my own fully-featured comet server (well it might be fun, but it’s a whole ‘nother project), and I don’t have a ton of money to spend on a dedicated server. Still I’d like to be able to release my little app to the world without fearing that my shared hosting provider is going to find its server overrun when my app gets a dozen users. So my question is, if you were on a limited budget and wanted to develop comet applications, what would you do? Is there any shared webhosting particularly friendly to comet? Are there any comet servers particularly friendly to shared webhosting?

  6. Alessandro Alinone Says:

    Erika:
    The architecture of an efficient Comet server is pretty different from that of a typical shared-web-hosting server (due to thread management, as you mentioned, and other aspects). So I wouldn’t expects current shared web hosting services to be Comet friendly. You should go with a dedicated partition, where you can install an actual Comet server.

  7. Erika N. Says:

    Thanks Alessandro. I suppose I shall have to bite the bullet and go for a VPS.

Leave a Reply



Copyright 2008 Comet Daily, LLC. All Rights Reserved