How to mitigate/force reconnect after clientsided “transport close” on socket.io?

113 Views Asked by At

I'm listening to a provided websocket (they offer some sample code) via socket.io like this:

const { io } = require("socket.io-client");
let socket = io('https://targetsite.com', { transports: ['websocket'] } );

socket.on("connect", () => {
 console.log("Connected to websocket");
});

socket.on("disconnect", (reason) => {
  console.log("Disconnected from websocket: " + reason);
});

socket.on('receivedData', (data) => {
    //program logic goes here
}

But I randomly get disconnected due to 'transport close' and somehow it does not automatically reconnect me (program logic is still executed but doesn't print new results). If I stop the script with ctrl+c and start it again the data is received again instantly.

Is there a way to force the reconnect/trigger the "socket.on('receivedData')" event once again?

Thanks a lot in advance.

0

There are 0 best solutions below