Re: [hybi] Is there a traffic jam?

Ian Hickson <ian@hixie.ch> Tue, 14 April 2009 05:26 UTC

Return-Path: <ian@hixie.ch>
X-Original-To: hybi@core3.amsl.com
Delivered-To: hybi@core3.amsl.com
Received: from localhost (localhost [127.0.0.1]) by core3.amsl.com (Postfix) with ESMTP id 16BB13A67F8 for <hybi@core3.amsl.com>; Mon, 13 Apr 2009 22:26:35 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -2.999
X-Spam-Level:
X-Spam-Status: No, score=-2.999 tagged_above=-999 required=5 tests=[AWL=-1.000, BAYES_00=-2.599, J_CHICKENPOX_46=0.6]
Received: from mail.ietf.org ([64.170.98.32]) by localhost (core3.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id q8lOhmaVaxal for <hybi@core3.amsl.com>; Mon, 13 Apr 2009 22:26:34 -0700 (PDT)
Received: from looneymail-a3.g.dreamhost.com (caibbdcaaaaf.dreamhost.com [208.113.200.5]) by core3.amsl.com (Postfix) with ESMTP id 1FE8F3A67D1 for <hybi@ietf.org>; Mon, 13 Apr 2009 22:26:34 -0700 (PDT)
Received: from hixie.dreamhostps.com (hixie.dreamhost.com [208.113.210.27]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by looneymail-a3.g.dreamhost.com (Postfix) with ESMTP id 776CD27B77; Mon, 13 Apr 2009 22:27:26 -0700 (PDT)
Date: Tue, 14 Apr 2009 05:27:25 +0000
From: Ian Hickson <ian@hixie.ch>
To: Greg Wilkins <gregw@webtide.com>
In-Reply-To: <49E40D7B.7090409@webtide.com>
Message-ID: <Pine.LNX.4.62.0904140510380.10339@hixie.dreamhostps.com>
References: <03BCE29D-7AA5-4128-9F61-446E0229479A@lindenlab.com> <E51D5B15BFDEFD448F90BDD17D41CFF105A0C46E@AHQEX1.andrew.com> <Pine.LNX.4.62.0904132352430.10339@hixie.dreamhostps.com> <E51D5B15BFDEFD448F90BDD17D41CFF105A0C476@AHQEX1.andrew.com> <Pine.LNX.4.62.0904140002360.10339@hixie.dreamhostps.com> <1cb725390904131712k292a4860pbd078bb251d3855b@mail.gmail.com> <Pine.LNX.4.62.0904140031040.10339@hixie.dreamhostps.com> <1cb725390904131752u5842c039wb3d75602c479fa45@mail.gmail.com> <Pine.LNX.4.62.0904140053050.10339@hixie.dreamhostps.com> <49E3E229.2060907@webtide.com> <Pine.LNX.4.62.0904140110040.10339@hixie.dreamhostps.com> <49E3F218.4080209@webtide.com> <Pine.LNX.4.62.0904140248050.10339@hixie.dreamhostps.com> <49E40D7B.7090409@webtide.com>
Content-Language: en-GB-hixie
Content-Style-Type: text/css
MIME-Version: 1.0
Content-Type: TEXT/PLAIN; charset="US-ASCII"
Cc: hybi@ietf.org
Subject: Re: [hybi] Is there a traffic jam?
X-BeenThere: hybi@ietf.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: Server-Initiated HTTP <hybi.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/listinfo/hybi>, <mailto:hybi-request@ietf.org?subject=unsubscribe>
List-Archive: <http://www.ietf.org/mail-archive/web/hybi>
List-Post: <mailto:hybi@ietf.org>
List-Help: <mailto:hybi-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/hybi>, <mailto:hybi-request@ietf.org?subject=subscribe>
X-List-Received-Date: Tue, 14 Apr 2009 05:26:35 -0000

On Tue, 14 Apr 2009, Greg Wilkins wrote:
> 
> But sharing HTTP connections is currently painful and difficult and 
> relies on a variety of javascript/dom hacks.  It also needs protocol 
> support on top of HTTP to support the multiplexing.
> 
> Websockets does not help with this.  It will still be painful to share 
> connections and protocols to multiplex will still need to be written on 
> top of it.  However, with the more approachable API of websockets, it is 
> likely that it will be used directly by widget/application developers 
> (and it is "sold" as such), so the probability of sharing/multiplexing 
> frameworks being widely used is small.

Come now. All you have to do to share a connection on the client is have a 
single object that does all the sending and receiving, with a 
corresponding piece of code on the server, which simply does three things:

1. To create a new "subconnection", sends a message, say "NEW" followed by 
a unique number followed by whatever information is needed to set up the 
remote end (e.g. the kind of widget), then hands back to the API caller a 
handle that is assocaited with the new unique number.

    "NEW 1 calendar-widget user=john@example.com"

2. When sending a message given one of these handles, sends the number 
followed by whatever the message is.

    "1 add-event,2009-01-01,Holiday"

3. When receiving a message, strips off the leading number, then hands the 
rest of the message to the corresponding listener.

Why would this be painful? You can do it in JavaScript in barely 20 lines 
of code:

  function MultiplexConnection(url) {
    this.socket = new WebSocket(url);
    this.connections = {};
    this.lastID = 0;
    this.newConnection = function(callback, s) {
      this.lastID = this.lastID + 1;
      this.connections[this.lastID] = callback;
      this.socket.postMessage('NEW ' + this.lastID + ' ' + s);
      return this.lastID;
    };
    this.postMessage = function (handle, s) {
      this.socket.postMessage(handle + ' ' + s);
    };
    this.socket.onmessage = function (e) {
      var handle = e.data.substr(0, e.data.indexOf(' '));
      this.connections[handle](e.data.substr(e.data.indexOf(' ')+1));
    };
  }

The server side is only midly more complicated because of having to handle 
the "NEW" connections.

If you want to use a multiplexing framework, then you can write one (and 
make it available to people, if you want others to use it too). Doing so 
on top of WebSockets is easy. Whether other people do it or not is neither 
your problem nor mine, as far as I can tell.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'