<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>lighty's life: COMET meets mod_mailbox</title>
    <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>COMET meets mod_mailbox</title>
      <description>&lt;p&gt;Some time ago we got a request on how to implement &lt;a href="http://alex.dojotoolkit.org/?p=545"&gt;&lt;span class="caps"&gt;COMET&lt;/span&gt;&lt;/a&gt; with lighttpd. I responded with a idea about a &lt;a href="http://article.gmane.org/gmane.comp.web.lighttpd/3765/match=comet"&gt;mod_multiplex&lt;/a&gt; which would allow the let the client open a &lt;span class="caps"&gt;COMET&lt;/span&gt;-channel and give the backend the possibility to feed multiple channels at once with the client to poll for new data.&lt;/p&gt;


	&lt;p&gt;Basicly it would separate the &lt;span class="caps"&gt;HTTP&lt;/span&gt; Request-Response cycle from the underlying connection. &lt;span class="caps"&gt;HTTP&lt;/span&gt; would be used to open the connection and reopen it in case it went away, but otherwise it would be just a data-channel for your JavaScript/AJAX content we want to send to the client when WE (the content-provider) want.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s quote my understanding of &lt;span class="caps"&gt;COMET&lt;/span&gt; again:&lt;/p&gt;


&lt;blockquote&gt;
My idea on this is to decouple the request from the &lt;span class="caps"&gt;COMET&lt;/span&gt; stream.
&lt;span class="caps"&gt;AFAI&lt;/span&gt; understand &lt;span class="caps"&gt;COMET&lt;/span&gt; it is a &amp;#8216;one-receiver-multiple-senders&amp;#8217; concept.
The channel (a &lt;span class="caps"&gt;HTTP&lt;/span&gt;-response) is kept-open while the server/app can send multiple responses even without browser interaction to the client.

&lt;/blockquote&gt;

	&lt;p&gt;This is what you see in a web-chat application or what is used to send out stock-quotes in real-time to a large group of users.&lt;/p&gt;


	&lt;p&gt;In the classic &lt;span class="caps"&gt;HTTP&lt;/span&gt; world you either have to &lt;strong&gt;poll&lt;/strong&gt; every one few seconds for new data or you keep the connection open and let it &lt;strong&gt;stream&lt;/strong&gt; you the data. &lt;strong&gt;Polling&lt;/strong&gt; is not instant and generates load even if no data is available. &lt;strong&gt;Streaming&lt;/strong&gt; binds FastCGI (or similar) backend to the connection which limits the number of parallel users.&lt;/p&gt;


&lt;h3&gt;Decoupling the Connection from the content&lt;/h3&gt;

	&lt;p&gt;We want&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;a persistent connection between to server and client to minimize the setup costs (keep-alive) and have immediate responses&lt;/li&gt;
		&lt;li&gt;a way to send data from a backend to multiple connections&lt;/li&gt;
		&lt;li&gt;to run a backend only to generate content&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;As this doesn&amp;#8217;t fit into the classic model, we have to break it a bit. Classic means:&lt;/p&gt;


	&lt;p&gt;1. server reads &lt;span class="caps"&gt;HTTP&lt;/span&gt; Request
2. server forwards the request to the backend and waits for its response
3. server sends &lt;span class="caps"&gt;HTTP&lt;/span&gt; Response to the client&lt;/p&gt;


	&lt;p&gt;As soon as the backends closes its connection to the server, the server will assume that there is no more data to transfer  and waits for the next client-request.&lt;/p&gt;


	&lt;p&gt;That&amp;#8217;s what we want to change. We want to decouple the backend from the client-connection. To take it one step further we want to implement something we already know every well: a mailbox.&lt;/p&gt;


&lt;h3&gt;Queuing messages in a mailbox&lt;/h3&gt;

	&lt;p&gt;If you are at home when the &lt;a href="http://imdb.com/title/tt0082934/"&gt;postman rings twice&lt;/a&gt; ... hmm, let&amp;#8217;s start again.&lt;/p&gt;


	&lt;p&gt;If you are at home when the postman delivers the a letter you can read it right away. He rings the bell, you say hello, take the letter, read it.&lt;/p&gt;


	&lt;p&gt;If you are working hard in the office, the postman will the deliver the letter too, just to your mailbox. Actually several companies deliver letters, packages, ... all to your mailbox. You&amp;#8217;ll pick the up when you come home.&lt;/p&gt;


&lt;h3&gt;mod_mailbox&lt;/h3&gt;

	&lt;p&gt;First we need a name for the mailbox. The client opens a mailbox on the server, the server ties the client connection to this mailbox and sends all the data it gets for this mailbox  right away to the client.&lt;/p&gt;


	&lt;p&gt;If the client drop the connection it either re-establishes or the server will remove the mailbox after some time.&lt;/p&gt;


	&lt;p&gt;A backend is started as usual and can send its response to multiple mailboxes and doesn&amp;#8217;t have to care about connections being up or down. It only delivers to mailboxes (queues) managed by the mod_mailbox.&lt;/p&gt;


	&lt;p&gt;In case the backend delivered new content into a mailbox while the connection was temporary closed it will be sent as soon as the client re-opens the mailbox.&lt;/p&gt;


&lt;h3&gt;Can I use it ?&lt;/h3&gt;

	&lt;p&gt;Not yet. This is a idea how to implement &lt;span class="caps"&gt;COMET&lt;/span&gt; in a nice, performing fashion. On the way to 1.5.0 it will be implemented. Meanwhile I&amp;#8217;m looking for comments on the idea and if it matches your needs for &lt;span class="caps"&gt;COMET&lt;/span&gt; and &lt;span class="caps"&gt;AJAX&lt;/span&gt;.&lt;/p&gt;</description>
      <pubDate>Mon, 27 Nov 2006 14:22:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3fede6a2-1042-4eed-ab14-f14b87db95e3</guid>
      <author>jan</author>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox</link>
      <category>lighttpd</category>
      <category>comet</category>
      <trackback:ping>http://blog.lighttpd.net/articles/trackback/2393</trackback:ping>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Marty</title>
      <description>Most of the web apps I work on would really benefit from the COMET model.

I had a 'heated debate' with a collegue about a month ago regarding the performance cost of AJAX - having COMET ability in lighttpd would, without a doubt, let me win the argument ;)</description>
      <pubDate>Sun, 24 Dec 2006 17:51:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:df7eca0b-2920-46e5-ae73-9d67cc285eaf</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2602</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by kathy</title>
      <description>If this became a reality, even if only a beta or alpha, I will switch to lighty immediately :)</description>
      <pubDate>Thu, 14 Dec 2006 09:37:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d0e3cc88-2748-4b61-9cd3-082eb7149d7d</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2564</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by zellster</title>
      <description>Additional work on the Java side with continuations:

&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html&lt;/a&gt;
&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html&lt;/a&gt;
&lt;a href="http://docs.codehaus.org/display/JETTY/Continuations" rel="nofollow"&gt;http://docs.codehaus.org/display/JETTY/Continuations&lt;/a&gt;
&lt;a href="http://www.webtide.com/downloads/whitePaperAjaxJetty.html" rel="nofollow"&gt;http://www.webtide.com/downloads/whitePaperAjaxJetty.html&lt;/a&gt;
</description>
      <pubDate>Wed, 29 Nov 2006 00:45:31 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8eed4f5a-1aca-438e-8be5-accc378a58a1</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2481</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by zellster</title>
      <description>Additional work on the Java side with continuations:

&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html&lt;/a&gt;
&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html&lt;/a&gt;
&lt;a href="http://docs.codehaus.org/display/JETTY/Continuations" rel="nofollow"&gt;http://docs.codehaus.org/display/JETTY/Continuations&lt;/a&gt;
&lt;a href="http://www.webtide.com/downloads/whitePaperAjaxJetty.html" rel="nofollow"&gt;http://www.webtide.com/downloads/whitePaperAjaxJetty.html&lt;/a&gt;</description>
      <pubDate>Wed, 29 Nov 2006 00:44:07 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1d7192ec-a727-49e8-b3ab-47362dac4b28</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2480</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by zellster</title>
      <description>Additional work on the Java side with continuations:

&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/07/the_grizzly_com.html&lt;/a&gt;
&lt;a href="http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html" rel="nofollow"&gt;http://weblogs.java.net/blog/jfarcand/archive/2006/10/writting_a_come.html&lt;/a&gt;
&lt;a href="http://docs.codehaus.org/display/JETTY/Continuations" rel="nofollow"&gt;http://docs.codehaus.org/display/JETTY/Continuations&lt;/a&gt;
&lt;a href="http://www.webtide.com/downloads/whitePaperAjaxJetty.html" rel="nofollow"&gt;http://www.webtide.com/downloads/whitePaperAjaxJetty.html&lt;/a&gt;</description>
      <pubDate>Wed, 29 Nov 2006 00:43:45 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bb442786-19b6-41ce-bcd2-4a048a8c97f6</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2479</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Greg Daniluk</title>
      <description>I've already implemented similar module for my web app. It works quite well with 1.4.* code. I cannot say it works great, because I haven't tested it with i.e. 10000 concurrent connections yet. Everything is implemented is C for best performance. 

The whole architecture is much simpler than cometd. You just need lighttpd and load additional module, that's all. 

During implementation I've faced few problems. 
1. When the connection is idle, a browser is waiting for data and lighttpd for content, the socket fd cannot be readable or writable state. Then the browser is disconnecting (a user leaves the page). But the socket fd will be only half-closed. The Linux kernel won't notify lighttpd process about that and lighttpd will keep half-closed socket fd. There is a need for periodic manual cleanup of these fds. That's my temporary solution. I guess that Linux kernel needs a patch - provide some SETSOCKOPT to change semantics of select or epoll so they notify a process about error when sock isn't readable or writable.
2. HTTP server keeps the state what might be a problem during restart, log rotate or design problem for some. No solution, have to live with that.
3. By default a browser will allow only 2 connections to a web page. When a user will open second or third window then it will just stall, the browser will wait for first connections finish. My module detects that case and redirects the browser to www1 or www2 address and so on. Of course www1, www2 addresses must lead to the same lighttpd instance.

These are other problems but they are already solved. I am going to release my module for free in 5-6 weeks when it will be better tested. My only concern is lighttpd stability. Some RoR folks discourage now using this server. I guess that stable lighttpd for comet applications might be a real hit.
</description>
      <pubDate>Tue, 28 Nov 2006 13:51:24 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:94eeb993-cd33-412e-984a-ffa1798e2eb6</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2452</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Felix</title>
      <description>great Jan - really awesome what you create here. Keep up the good work</description>
      <pubDate>Tue, 28 Nov 2006 12:45:27 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:eafc169c-86e7-45c1-ab92-b0ea37a509d5</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2448</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Jan Kneschke</title>
      <description>Looks like Bayeux is already doing want I was thinking about, and the best part: Dojo already supports it.

</description>
      <pubDate>Mon, 27 Nov 2006 23:59:08 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a289f73f-9431-4de6-832b-a6a16cb77c79</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2418</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Fobax</title>
      <description>How would such a system scale to multiple lighty/comet servers? We're looking into such a system, but we'd want to be able to support 50,000 persistent connections, which is likely more than one lighty instance can handle.</description>
      <pubDate>Mon, 27 Nov 2006 22:09:59 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d69b135d-f2d5-4d06-b2fd-e46863d7fd5d</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2414</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Xantus</title>
      <description>D'oh  Sorry about the double post.  The comment form makes it so easy to do it! :)</description>
      <pubDate>Mon, 27 Nov 2006 19:08:08 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:804148fa-a8c7-46c8-bdea-ec00a82eb0a0</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2407</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Xantus</title>
      <description>You should join us in development of cometd.  Check out our site at cometd.com  </description>
      <pubDate>Mon, 27 Nov 2006 19:07:42 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2aac851c-d9c3-42e1-b096-b6c9374b5986</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2406</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Xantus</title>
      <description>You should join us in development of cometd.  Check out our site at cometd.com  </description>
      <pubDate>Mon, 27 Nov 2006 19:07:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:814437c6-3e54-4922-acc5-b96549eb0e0c</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2405</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Thijs van der Vossen</title>
      <description>Yes, this is indeed how we want to do it. Very nice.</description>
      <pubDate>Mon, 27 Nov 2006 19:02:28 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bbf78e66-acc4-41fe-95b7-60182a5683ed</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2404</link>
    </item>
    <item>
      <title>"COMET meets mod_mailbox" by Scott</title>
      <description>Some additional things that will make these even easier (and more efficient) for backend development: support for mailbox groups.&lt;br&gt;&lt;br&gt;

For example, backend tells lighty, add these mailbox ids to group "chat". From then on, the backend can address messages to the group "chat" without having to explicitly specify all the mailbox ids. This means less overhead in backendlighty communication and less parsing lighty has to do.&lt;br&gt;&lt;br&gt;

A group will be created when the first mailbox id is added to it. The group will be removed when the last mailbox id is removed from it (either by the backend explicitly, or by lighty expiring them).&lt;br&gt;&lt;br&gt;

For extra points, a send-to-group-excluding-these-mailboxes feature would mean for something like a chat server, you can have all clients in a single group, and the client (via the backend) can easily send to the group without receiving the message itself. The client can provide local echo like IRC does.&lt;br&gt;&lt;br&gt;

Named groups might also mean the backend could be restarted and continue where it left off without having to keep track of all the mailbox ids, but that's quite a simplification and it would probably need the membership info for other uses anyway.</description>
      <pubDate>Mon, 27 Nov 2006 17:12:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d87ed23b-4b23-42f5-9aea-3303eefc3cb2</guid>
      <link>http://blog.lighttpd.net/articles/2006/11/27/comet-meets-mod_mailbox#comment-2397</link>
    </item>
  </channel>
</rss>
