I use this code to connect to NodeJS web socket:
useEffect(() => {
let futureResponseFeedAddress = "ws://localhost:/endpoint";
const futureResponseClient = new W3CWebSocket(futureResponseFeedAddress);
props.onUpdateOrderbookWebsocket(futureResponseClient);
futureResponseClient.onopen = () => {
console.log("WebSocket Client Connected on " + futureResponseFeedAddress);
};
futureResponseClient.onmessage = (message) => {
..............
};
futureResponseClient.onclose = closeEvent => {
console.log("response messages websocket closed.");
}
return function cleanup() {
futureResponseClient.close();
};
}, []);
But after around 1-2 minutes the messages are not received. Looks like I get timeout. Do you know how I can configure the web socket to be always open?
My suggestion is you should use socket.io for doing any real-time communication . It uses http long polling ,upgrades the connection to websocket if needed and it has also good browser support . It is much realiable too. But there are some drawbacks , your server need to use socket.io server api .
There is no need to re-invent the wheel .