My app is in browser, which connects to socket.io server, it works fine, but, when client disconnect for a while ( sleep for example ) , the connection will be automatically closed by server, this is the default behavior for socket.io
How can I re-establish the connection at client side without refreshing the page? is there a status that would tell me that connection is currently off at client side? so that I re-connect when necessary?
I can't rely on an event, I think I need to know if connection is on or off in an easy way locally .. well?
socket.io does reconnect automatically. To check if your socket is connected check socket.socket.connected has Boolean value. See socket.socket.connecting if you are trying to connect.
var socket = io.connect(''); //reconnects by default
console.log(socket.socket.connected);
console.log(socket.socket.connecting);
Related
We are facing a problem where clients connect to the server and a socket is created and is working.
But for example if the server goes down / we upgrade the version and restart the server,
the sockets are lost until clients refresh the page.
Our clients are passive, and will not refresh the page often leaving the server disconnected for updates and notifications.
We would like to know if it is possible to store the sessions somehow like a mongo store.
I created an OPC-UA server based on Node-OPCUA and node.js and several clients have wrong or empty parameters when connecting. This causes malfunctions on clients side.
Is it possible for server to override or correct the client configuration parameters so it works correctly even when the client is wrongly defined?
I am particularly interested in overriding session and connection timeouts.
Clients may disruptively disconnect, so the server does no close that connection. How do I get rid of those zombie connections? Sessions are usually closed due to timeout, but not connections.
You can't do that because it's again the OPC-UA protocol. The client should be able to open a connection with its own parameters.
If you detect that the client is wrongly configured you should throw a ServiceException instead.
I'm new in socket-io. socket-io working fine but keep sending connect request infinitely to server.
Here is my client ts :
private url = environment.socketServer;
constructor() { this.socket = io(this.url) }
The usual reason for a socket.io client to try to connect over and over is if the socket.io version on the server and client are not compatible. The client connects, the server finds the version is incompatible and drops the connection and the client then tries to connect again, over and over.
Other possible issues:
Server infrastructure (such as load balancers, proxies, firewalls, etc...) are not properly configured to allow webSocket connections.
You're trying to connect to a cluster, but it isn't configured for sticky connections that will "bind" a socket.io client to the same server.
You're confused about how a socket.io connection starts. It is normal for the client to start with a couple web connections in a row (polling) until it realizes that both sides support a webSocket and then socket.io switches over to webSocket.
Say there are 5 open tabs in a single browser, and I am using Node.js and Socket.io. The client and server exchange packets to maintain (establish) communications.
Will connection with the client be lost if of one tab is closed?
How can I determine if the user closed the browser?
Yes, socket.io establishes a new connection with every tab. You're going to want to look into using session cookies to figure out which user the current socket.io connection is communicating with.
if the server is connected to a websocket client using socket.io, what are the events that would cause the client.id to change?
the server reset
the client opening a new connection
are there others, such as timeout?
When client connects to the socket.io server, then new id is generated for the connection. And there is no other place where id is generated.
Server reset, client opening new connection, timeout (and reconnection) - all of these trigger creation of new id, because actually client has to create a new connection to the socket.io server.