<div dir="ltr">Hello,<br><br>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.<br>
<br>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.<br><br>
My thought is something like this:<br><br><div style="margin-left: 40px;"><span style="font-family: courier new,monospace;">window.addEventListener( "load", function() {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">
  if( sessionStorage.</span><span style="font-family: courier new,monospace;">resumeToken</span><span style="font-family: courier new,monospace;"> != undefined ) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    // this part is sketchy...<br>
    try {<br style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">      con = new WebSocket( sessionStorage.resumeToken );<br>    } catch(e) {<br>      // too much time elapsed, invalid token<br>
      </span><span style="font-family: courier new,monospace;">con = new WebSocket( "ws://..." );</span><br><span style="font-family: courier new,monospace;">    }<br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">  } else {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    con = new WebSocket( "ws://..." );<br style="font-family: courier new,monospace;">
</span><span style="font-family: courier new,monospace;">  }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">
}, false );</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">window.addEventListener( "unload", function() {<br>
  </span><span style="font-family: courier new,monospace;">// timeout could be defined by UA, and connection closes after that time</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">  sessionStorage.</span><span style="font-family: courier new,monospace;">resumeToken</span><span style="font-family: courier new,monospace;"> = con.pause(); </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}, false );</span><br><br></div>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.<br>
<br>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.<br><br>Best,<br><br>Harlan<br></div>