BitMEX / api-connectors

Libraries for connecting to the BitMEX API.
https://www.bitmex.com/app/restAPI
910 stars 795 forks source link

[nodejs] WebSocket closed unexpectedly. #150

Open imnotjoel opened 6 years ago

imnotjoel commented 6 years ago

Hi guys, I have the following issue. Hope you can give me a hand!

WebSocket [INFO]: Connected.
Connection opened.
Client initialized, data is flowing.
{"op":"subscribe","args":"order:XBTUSD"}
WebSocket [ERROR]: WebSocket closed with code 1006
Connection closed.
WebSocket [INFO]: Retry in 1000 ms
WebSocket [INFO]: Reconnecting...
WebSocket [ERROR]: Unexpected response from server [400]: {"error":"Nonce is not increasing. This nonce: 1528139622410000, last nonce: 1528139622410000"}
WebSocket [INFO]: The WebSocket will terminate. Please manually reconnect.
WebSocket [ERROR]: WebSocket closed unexpectedly.
Connection closed.
/Users/nosoyjoel/Documents/api-connectors-master/official-ws/nodejs/lib/createSocket.js:70
    if (!listeners.length) throw new Error('WebSocket closed. Please check errors above.');
                           ^

Error: WebSocket closed. Please check errors above.
    at WebSocketClient.wsClient.onend (/Users/nosoyjoel/Documents/api-connectors-master/official-ws/nodejs/lib/createSocket.js:70:34)
    at WebSocket.instance.on (/Users/nosoyjoel/Documents/api-connectors-master/official-ws/nodejs/lib/ReconnectingSocket.js:45:12)
    at WebSocket.emit (events.js:182:13)
    at closeConnection (/Users/nosoyjoel/Documents/api-connectors-master/official-ws/nodejs/lib/ReconnectingSocket.js:71:23)
    at IncomingMessage.response.on (/Users/nosoyjoel/Documents/api-connectors-master/official-ws/nodejs/lib/ReconnectingSocket.js:84:9)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1090:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

I tough it has to do with my connection.. But then I tried to run it in a ECS instance and I had the same issue.

Thanks in advance!

ryanfox commented 6 years ago

Code 1006 means the connection was closed improperly. See this stackoverflow answer for more info.

Does it happen consistently every time, or is it sporadic?

imnotjoel commented 6 years ago

Hi @ryanfox It happens always after ~4 minutes

ryanfox commented 6 years ago

@dannyluong408 it's not unexpected that the socket would get disconnected after that amount of time. If you want to reconnect it, you could wrap your code in a retry block.

@imnotjoel is your network connection stable in general? One way you could get more information is to enable DEBUG - this will produce large amounts of output though.

lewisbarber commented 6 years ago

I have this problem every single time. I don't seem to be able to catch the exception either for some reason (using Node lib).

annugaurish commented 6 years ago

I have solved this problem by making changes in createSocket.js Added following code in the createSocket function

wsClient.onreconnect = function() { wsClient.url = makeEndpoint(options);

debug('Reconnecting to BitMEX at ', wsClient.url);

};

and in ReconnectionSocket.js added the following line in reconnect prototype this.onreconnect();

this is to be added before this.open(this.url);

sandybradley commented 6 years ago

Dear Bitmex community,

Thank you annugaurish for your corrections. My Bitmex node client now does not crash but still reconnects every few seconds with a 1006 error. The same code was working fine 3 days ago. Can anyone explain why the client is continually disconnecting all of a sudden?

Faithfully,

Alexander

Xavier59 commented 6 years ago

Thanks @annugaurish ! This solution fix the problem. Pull request needed :)

annugaurish commented 6 years ago

Welcome:)

pabx06 commented 6 years ago

same issue here a bit painful to have my bot stop completely from that crash...

martincpt commented 6 years ago

I solved this problem by sending a raw ping every minute. The client library has a socket variable holding the websocket object. So just setup an interval and send the ping like this: client.socket.send('ping');

You may recieve messages like:

Unable to parse incoming data: pong

But its not a big deal i guess.

ever4cys commented 6 years ago

I'm in the middle of trying @annugaurish 's solution. Let me see if it's working well. But I wonder why there's no pull request about this?

maxkoyubi commented 6 years ago

We're having the same issue and @annugaurish solution does not seem to change anything.

honey96dev commented 5 years ago

I tried to fix with @annugaurish`s method, but that solution does not seem to change anything.

nvrossett commented 5 years ago

I solved this problem by sending a raw ping every minute. The client library has a socket variable holding the websocket object. So just setup an interval and send the ping like this: client.socket.send('ping');

You may recieve messages like:

Unable to parse incoming data: pong

But its not a big deal i guess.

This work to me

I just made a small change in my local in the file: https://github.com/BitMEX/api-connectors/blob/master/official-ws/nodejs/lib/createSocket.js#L38

Adding an if, to not return this message if the answer is pong!

Resolved issue and no longer issues annoying message

Thanks for sharing

image

CRLakeDev commented 4 years ago

I have noticed re-connection errors and tried the above method but did not solve the issue.