[whatwg] maintaining WebSocket connections between page loads

Harlan Iverson h.iverson at gmail.com
Fri Aug 1 13:39:45 PDT 2008


Hello,

I am an implementor of BOSH and interested in WebSockets as future option
for browser based XMPP connections. I think a useful feature of BOSH is the
ability to send a pause request to the server, which effectively increases
the amount of time that can elapse before it considers a client timed out; a
client then resumes by making a normal request with the same session ID and
the request ID incremented as usual. This is useful/needed because BOSH is a
sequenced protocol. Importantly, it enables a use case of maintaining a
'persistent' connection between page loads.

Is there any equivalent mechanism in WebSockets to produce a 'persistent'
connection between page loads? Combined with sessionStorage this would be
very useful for an application such as Facebook Chat.

My thought is something like this:

window.addEventListener( "load", function() {
  if( sessionStorage.resumeToken != undefined ) {
    // this part is sketchy...
    try {
      con = new WebSocket( sessionStorage.resumeToken );
    } catch(e) {
      // too much time elapsed, invalid token
      con = new WebSocket( "ws://..." );
    }
  } else {
    con = new WebSocket( "ws://..." );
  }
}, false );


window.addEventListener( "unload", function() {
  // timeout could be defined by UA, and connection closes after that time
  sessionStorage.resumeToken = con.pause();
}, false );

Personally I'm not a fan of the WebSocket constructor initiating the
connection immediately, but I'm sure there are reasons for it being like
that. My preference would be to have connect and resume methods to
complement disconnect and pause, respectively.

I haven't thought through security implications of this sort of mechanism,
but at the very least it seems safe if tokens can only be used within the
same origin.

Best,

Harlan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20080801/2539a46a/attachment-0001.htm>


More information about the whatwg mailing list