Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing websockets between pages?

Tags:

websocket

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.

like image 577
Joshua Hutchison Avatar asked Mar 06 '12 06:03

Joshua Hutchison


People also ask

Can WebSockets be spoofed?

If you build your websocket over HTTP, then yes, it is completely possible for a third party to spoof the connection (and also to eavesdrop). If your HTTPS/WSS system does not properly validate certificates, then that also can be spoofed.

Can I reopen a closed WebSocket?

Once the original connection has been closed, you need to create a new websocket object with new event listeners: var SERVER = 'ws://localhost:8080' var ws = new WebSocket(SERVER) ws.

Does WebSocket close on page refresh?

User presses refresh. Browser closes down all resources associated with the original page, including closes the webSocket connection.

Why WebSockets are not reliable?

It has built-in means of detecting transmission errors or packet corruption and attempting retransmissions in those cases, but delivery can still fail. It guarantees that if the packet is not delivered, the caller will get an error so the caller can know. Since websocket is built on top of TCP, it has the same issue.


3 Answers

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 !

like image 182
WiMantis Avatar answered Nov 12 '22 21:11

WiMantis


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...

like image 41
ATOzTOA Avatar answered Nov 12 '22 21:11

ATOzTOA


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.

like image 29
kanaka Avatar answered Nov 12 '22 21:11

kanaka