When I try to connect my React client to Huobi websocket, connection immediately closing.
In nodejs I can connect to Huobi websocket without any configuration.
I've tried at postman and it connects.
Tried at https://www.piesocket.com/websocket-tester but it did throw same error and closed immediately. This is error: huobi websocket error at client
This is the sample code for both nodejs and clientside:
const websocket = new WebSocket("wss://api.huobi.pro/ws");
websocket.onopen = () => {
console.log("connected");
subscribe(websocket);
};
Most of the code below comes from this answer to this question which is similar to yours: Client JavaScript Problem in unzipped Gzip data form webSocket
pingto which it expects apongin reply otherwise it will close the connectionThe code uses / imports
fflatewhich works in browsers and is smaller and faster than other compression libraries.I've added a little bit of error handling to the code. This could be expanded upon to attempt a reconnect to the web socket on an error or if closed.
More information on handling connections to Huobi websockets can be found in their documentation.