How can I make socketio close connection immediately on page refresh? - socket.io

I have implemented a chat service but it seems if a user keeps refreshing the page fast enough it can connect to itself. It is not exactly itself but the id of its previous session. Issue happens because on fast refreshes browser does not trigger io.disconnect. I have tried to solve it by attaching disconnect code to onbeforeunload event but it doesn't make much change. I don't want to fiddle with pingtimeout and pinginterval because those might interfere with reconnection abilities. Any ideas?

I believe you are mistaken. Those duplicate connections will be disconnected eventually. For connections that are established with the polling protocol it takes about a minute to detect a disconnected client.

Related

What happens with WebSocket connections when a phone's screen locks?

When a phone browser has an open connection, and the user locks the screen, then at a certain point they will no longer have a WebSocket connection.
What events are fired when this happens? Is the WebSocket.onerror or WebSocket.onclose handler called, and if so, does this happen when the screen locks/the app is suspended, or when the app comes back up again?
(And bonus question: is this standardised, or do browsers behave differently, and if so, how?)
I've done some testing myself, and the answer seems to be: no events are fired. Although the connection does drop, no error or close events are fired, not even when the browser comes back up. Therefore, the main way to deal with this appears to be to periodically check the connection status, and reconnect if need be - with exponential back-off in case the connection drops server-side. (Or to have a library do this for you, though I haven't found a properly maintained client-side browser-based WebSocket library that does this yet.)
This seems corroborated by the author of this article:
Mobile devices introduce a new category of connection issues; if a mobile device is locked, goes to sleep or the application is moved to the background, an active WebSocket connection may become unresponsive and not close itself properly.

Is it possible for a websocket frame to fail to arrive?

As I understand it, Websockets use a Ping to detect that they are still connected. Except of course Chrome which leaves it to apps to do the ping themselves.
I'd like to understand if its possible for a connection to become unstable between pings such that a frame of data is not received... but to stabilize again by the time the next ping is sent. In other words: is it possible to have an apparently good websocket connection, but for data to fail to arrive?
Question relates to Is it possible to miss websocket events which remains unanswered and side-tracked into long-polling and socket-io.
Thanks!
This is heavily dependent on the client software (browser) that you use.
The websockets depend on a TCP connection which will make sure the message arrives to destination. Except if the network connection is down, of course.
However, some clients (browsers) will suspend the inactive tabs and will not process the events. If your page is inactive, it "may" fail to send data to the server because it will not be executed at all. On the other hand, it "may" also fail to receive data because the handler will not be executed at all.
Meanwhile, even if inactive, the machine will still receive the ping packets. So it is really about whether or not your client software gives it back to your code or not.

Socket.io data loss when Internet speed drop

I am using socket.io 1.4 and I want to know that what happens in this scenario:
The client Emits like this:
Socket.emit('test',data);
The client does 3 emits to server but suddenly Internet speed drops and those emits may not get to server
But after a while the Internet speed rises again but what will happen to previous failed emits?
They will be emitted again automatically?
How should I handle that
Websockets use TCP, which is in general a reliable protocol. There is not exactly such a thing as "The internet speed dropped and I lost some messages." If some messages are lost they will be automatically retransmitted at the TCP level. If retransmission fails completely, the connection will be reset.
So what you really are asking is how socket.io handles this. And the answer is that it has some amount of reconnecting logic, and you may also want to monitor the connection in case it resets (hook up a listener for the disconnect event on the socket), if you want to take some extra action (like notify the user).

Does Ajax connection disconnect at some point?

My Co-worker told me that AJAX connection alive until a user closes his/her browser. As far as I know, ajax connection closes its connection when its request has completed. I tested it with Firebug and a HTTP monitoring tool, I noticed that AJAX connection closes itself.
Is he correct????
Ajax is just like any other request, when it completes the connection is closed. Your colleague is wrong.
Note : There are connection types that allow you to keep the connection open indefinitely
Break down what AJAX is -- and XMLHttpRequest. It's a connection to a URI endpoint for some resource (image, text, whatever). Your browser closes the HTTP connection as soon as it's done.
Ajax connections closed after receiving data or if you close tab, then connections will force closed.
Here described Ajax life cycle.
Late to the party here, but this regards an issue I'm actually dealing with right now...
I have a web server running on a severely resource constrained platform (128k Flash, 48k RAM) so it can only handle one connection at a time. Further connections are not handled until the current one is closed. Also, it does not force Connection: close on certain URLs because of a low latency requirement. In general, only one thing talks to the device at a time.
AJAX connections follow whatever rules the browser sets for other connections. In my case, I'm testing a web page that uses AJAX to read one of the keep-alive-allowed URLs once per second, and the browser does not close the connection until several seconds after the window is closed. As a result, other clients wait indefinitely.
So don't assume that XHR connections are closed when complete. They might not be; Firefox 21 sure isn't closing them.
My current problem is that I want to force my AJAX requests to close the socket on completion, and I'm using jQuery's .ajaxSend() pre-send hook to set the Connection: close header. The AJAX seems to be working, but when another client tries to connect, it gets "connection reset by peer", so I'm wondering if Firefox doesn't notice the "Connection: close" header on the XHR request and keeps its end of the socket open (until it times out after approximately three seconds) even after the server has closed its side.
Using JQuery to make AJAX requests, some connections are maintained until the following AJAX call. This can become a real problem when the server holds streams open until the response's close event fires.

Is there any technology to kill the user login session if user close the browser?

This should work if multiple windows are open.
My application is in J2EE.
I already tried some javascript like on window.unloadn but this kind of solution is not solid.
If you mean on the server side, the best way is to implement a heartbeat. If none of the windows from this webapp are open, nothing sends a heartbeat, and a server after some time will know the app is disconnected.
If you mean on the client side, there's no 100% reliable way, since the browser might die from external causes (kill/crash/reboot), but a timed-out cookie that is updated regularly in a heartbeat-like fashion might work
HTTP is a stateless, meaning you can't tell when a browser is closed.
Your attempt to use window.onunload does not always work because the browser could exit before executing your event handler.
The technology used to find out if a user has closed their browser (or a best guess) is to use timeouts. Sessions will automatically timeout (this is configurable) - in asp.net the default is 20 minutes. If there is no activity, the timeout kicks in.

Resources