NodeJS socket.io-server doesn't fire 'disconnect' or 'close' events when client out of internet - disconnect

NodeJS socket.io-server doesn't fire 'disconnect' or 'close' events when client out of internet.The server could not listen 'disconnect' when client out of internet.How to do it????

when client is out of internet socket.io fire disconnect event after some time go to node_modules -> socket.io -> lib -> manager.js edit your
'close timeout' , 'heartbeat interval' , 'heartbeat timeout' values according to your requirement.

The migration doc didn't meet expectations, they said the are providing backward compatibility for heartbeat timeout and heartbeat interval, http://socket.io/docs/migrating-from-0-9/#configuration-differences
But it didn't work for me..
So guys please use this in latest version of socketio
var io = require('socket.io').listen(httpd, {
log: true,
"transports":["polling"],
"pingInterval":10000,
"pingTimeout":15000
});

Related

React WebSocket on server, Error during WebSocket handshake

I have made an application that uses a WebSocket. I want to run this application on a PLESK Server, but I get this error message 'WebSocket connection to 'wss://sub.domain.com/' failed: Error during WebSocket handshake: Unexpected response code: 404' when I visit the website.
After reading a few blogs, I came to the conclusion you have to adjust something in the NGINX settings, but this also appears to have no effect, or I have to do it wrong, that is also possible. (Another blog)
At the moment our API is installed on a subdomain (with the NodeJS add-on in plesk) and it also works. As soon as I start up the files locally I can connect to the WebSocket and the API so it must be up to the PLESK Server I think. At this moment only the NGINX 'Proxy mode' is off and the 'Additional nginx directives' empty.
Perhaps one of you is familiar with this problem?

How do I refresh the token in a cometd client talking to a Salesforce Platform EventBus?

I'm using cometd to listen to platform events generated in Salesforce. My cometd client configuration code looks like
this.client.configure({
url: `${this.org.instance_url}/cometd/46.0`,
requestHeaders: {
Authorization: `Bearer ${salesforceToken}`
},
appendMessageTypeToURL: false
});
where the salesforceToken is obtained using the refresh token. This all works fine for a while but if there are no events for a considerable period of time (anecdotally anywhere between 6-24 hours), then my client seems to expire and no events are received. If I refresh the token and restart my listener, things start working again.
Is there a way to keep the listener active other than writing some sort of timer to restart the process every few hours after inactivity ?
You do not have to refresh the token again
Whenever there is no activity on the channel, the server closes the connection after a specific time.
In that time client receives a 403 (Unknown client) status code, and the client has to handshake again withing 110 seconds.
By default, CometD tries to reconnect without any user interaction and If the client doesn't reconnect within the expected time, the server removes the client's CometD session.
Once the connection gets reconnect, all the channel subscriptions will be removed by ComedD, and we have to subscribe to the channel again in order to receive events.
In order to do so, we have to make use of meta/Handshake callback to resubscribe to the channel again.
see https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/using_streaming_api_client_connection.htm, paragraph "After Invalid Authentication"
"Streaming API regularly validates the OAuth token or session ID while the client is connected. If client authentication is not valid, the client is notified with the 401::Authentication invalid error and an advice field containing reconnect=none. After receiving the error notification in the channel listener, the client must reauthenticate and reconnect to receive new events."

Camel PAHO routes not receiving offline messages while connecting back

I'm using apache camel xml based paho routes for the subscription, publication process. While online, everything works fine. But I'm not able to receive the offline message.
I have set the following.,
Constant Client ID
Clean Session is FALSE,
Both subscribed & published with QoS 2
With the standalone Program, it's getting all the offline messages. With the camel route it's not happening.
Finally, I was able to solve this one manually.
Camel PAHO Client is not populating the callback function before performing the broker connection. They are doing it only when the connection is made.
So, once the connection is success then the broker just sends all the offline messages. In this case, our client does not have callback handlers to handle these messages. So they are lost.
Other clients (IoThub Client) which uses the PAHO internally is doing it right by setting the callback and initiating the connection.

Socket io client reconnect handler

I need to implement a reconnect handler which first request a token over ajax from another server which is used as authentication on the websocket server.
Step 1: connect to http://test.com/{my_temp_token}
Step 2: on disconnect get new token
Step 3: connect to http://test.com/{my_new_temp_token}
Does socket.io automatically reconnect on disconnect? When yes how to implement an own reconnect handler, when no best way to handle an auto reconnect with an ajax loaded authentication token.
Socket io has a list of event listeners that it supports out of the box. If you want to do something custom on reconnect then you would just do...
this.socket.on('reconnect', e => {
// Socket has successfully reconnected
console.log(`It took ${e} tries to reconnect`);
// Do something custom like requesting a new token here...
});
Hope that helps!

Reset Socket.io Client connection

I write a Google Chrome Extension that uses Socket.io capabilities. More precisely, it's an AngularJS app with angular-socket.io on board.
I allow users to set up socket.io server address and port in the options page. Everything works fine until a user wants to change the address because I don't know how to reset the Socket.io connection. Ideally, I would like to close previous connection and reconnect with new connection without restarting the app (remember, it's an Chrome Extension, I cannot restart it by itself).
So my question is: How to reset the Socket.io client connection and reconnect with new DSN?
PS. I'm using socket.io-client#1.3.6
You are using angular-socket.io, so you inject socket.io (with a given config) as a service into your angular app. Assuming your service is called socketIOService you can do following:
Disconnect on client-side with socketIOService.disconnect();
Reconnect with socketIOService.connect(SERVER_IP, {'force new connection': true});

Resources