12

Is there a way to open a websocket on one page and then reuse it on another page (within the same tab, after a user clicks on a link, for example) rather than have to open a new websocket after each page load? Is browser storage able to hold onto an open socket?

The aim is to be able to keep to one websocket per user (or tab) and it would be great to do this without needing to move between pages in a non-traditional way, for example loading content into a div using Javascrpt each time the user interacts with the page.

Joshua Hutchison
  • 567
  • 8
  • 17
  • 1
    When a websocket connection is created some state exists which both sides of the connection must keep track of for the the connection to exist. I'd think that if you stored the client side state of a connection in local storage (note that the client side of the websocket system must be modified to allow this most or even all websocket clients don't support this as far as I know), you might be able to reopen the connection latter or even on another page (as long as the server hasn't dropped its half of the state). So it must be possible but how? I'd like to see a working sample. – Dan D. Mar 06 '12 at 06:50
  • I imagine that to "re-create" the websocket like that the server would have to support it as well. Or more particularly the library used to implement websocket support on the server. I suspect most implementations on the server would wind up the websocket when it detected the underlying TCP connection was closed - although the changes to the client you suggest _could_ keep it open. – Joshua Hutchison Mar 06 '12 at 09:36
  • 3
    Yup, **a websocket connection does not out live the tcp connection created as transport**, well, I thought they were cleverer. – Dan D. Mar 06 '12 at 10:23
  • possible duplicate of [Do Shared Web Workers persist across a single page reload, link navigation](http://stackoverflow.com/questions/9336774/do-shared-web-workers-persist-across-a-single-page-reload-link-navigation) – kanaka Mar 07 '12 at 14:07
  • 3
    @kanaka, no it isn't. This is pertaining to websockets and your link is about web workers which are two separate technologies. – Jeff LaFay Jul 08 '12 at 20:18
  • @jlafay, the question titles are different but they are after the same answer: being able to keep a WebSocket connections open even when the page changes. – kanaka Jul 10 '12 at 14:17

4 Answers4

10

The answer is no.

Even if the socket is not explicitly closed by calling mySocket.close();, the socket will be closed by the browser on reload.

I tried storing the Web Socket object in local storage and using it to retrieve data again. The object returned is valid, but the connection in not there anymore. Because, when the page reloads, the socket is ungracefully terminated.

Message at server side says:

[Errno 10053] An established connection was aborted by the software in your host machine

There you go...

ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • I was curious what would happen if you put a websocket (or reference thereto) into local storage. Thanks for trying it out and solving the mystery. – Joshua Hutchison Feb 14 '13 at 00:22
8

A different approach would be to keep the user instead of the socket across different pages. By that i mean you store the client's ID in a cookie with javascript, each time the user try to open a new socket from any of your website pages, you send this ID to the server and then the server have a way to know that this new connection is from the same user.

I've done that in a recent project and it work perfectly :) Depending on what you are planning to do, you can keep the state of the user on your server with his ID, or store it in an other cookie, or event use flash to store it in a shared object !

WiMantis
  • 376
  • 3
  • 11
  • Yes, that's entirely possible, and would have been a perfectly valid solution (and one we discussed) but the underlying architecture relied on the websocket remaining open in order to meet some of the functional requirements. Of course the issues that would have been caused by opening and closing sockets could have been mitigated but ultimately it was decided it better to instead keep the socket open and dynamically load content based on the location property of the URL instead. Not sure it is the better way but c'est la vie. – Joshua Hutchison Feb 13 '13 at 23:50
  • @WiMantis which connection manager for websocket are you using at server side? – java seeker Apr 03 '14 at 03:20
2

Shared Web Workers would allow you to share a WebSocket connections for multiple tabs that are loaded from the same origin/site.

Shared Web Workers are only currently supported on Chrome, Safari, Opera.

thirtydot
  • 224,678
  • 48
  • 389
  • 349
kanaka
  • 70,845
  • 23
  • 144
  • 140
  • Sorry, my question wasn't clear enough. When I said different pages I should have specified within the same tab. I've edited my question to hopefully make that clearer. Thanks for having a go, though. – Joshua Hutchison Mar 07 '12 at 03:44
  • @Joshua: ah, then you are asking the same thing that I asked a little while ago: http://stackoverflow.com/questions/9336774/do-shared-web-workers-persist-across-a-single-page-reload-link-navigation/9337749 There is no definitive answer to that one either, I intend to test at some point to see if it works in Chrome/Safari. – kanaka Mar 07 '12 at 14:06
  • Yes, the questions are similar other than I was hoping maybe there was something different about websockets that meant I might get a different answer. – Joshua Hutchison Mar 08 '12 at 04:27
0

A websocket connection is essentially a HTTP connection which was upgraded from HTTP to WSS. It connects over TCP and once a client/server leaves it, it will be closed.

You can store the connection data into the localStorage/cookies/anywhere else, but that is all it will be. The connection data, the connection itself will be terminated by the server as soon as the page reloads.

So, the answer is no. Connections cannot be persisted on page reloads.