coinbase-pro-node icon indicating copy to clipboard operation
coinbase-pro-node copied to clipboard

websocket connection stops working after few hours

Open adityamertia opened this issue 8 years ago • 9 comments

Hello, I am getting latest price updates from GDAX using websocket. But the websocket stops responding after few hours. Is there an issue with websocket? I have tried this from multiple servers to rule out server/network issues. I make websocket connection as below const websocket = new Gdax.WebsocketClient(['LTC-USD', 'ETH-USD']); Is heartbeat true flag needed as below to avoid this issue? const websocket = new Gdax.WebsocketClient(['BTC-USD', 'ETH-USD'],{heartbeat: true}); Is there a way to avoid websocket disconnections and keep it running forever? Thanks Aditya

adityamertia avatar Jan 05 '18 05:01 adityamertia

Hi @adityamertia

If you use the latest version from the master branch, the heartbeat channel is automatically subscribed (i.e. it's always on).

npm install coinbase/gdax-node

However, just because the heartbeat is subscribed doesn't mean that you're guaranteed to get a perfect websocket stream. It's still your responsibility to monitor the heartbeat messages and recognize inactivity. The heartbeat is published every second per product, so if you don't see a heartbeat after an extended period of time, it's probably a good sign that you need to reconnect to the websocket. For my personal bot, I consider the websocket dead if the last heartbeat was > 15 seconds ago but I've seen others wait for 30 seconds.

I think the reason for these "disconnects" is because when GDAX publishes a new release to their servers, they might not guarantee that existing websockets remain subscribed. Someone at GDAX should be able to better clarify this.

Nonetheless, one of the things that I'd like to add to gdax-node is to make the WebsocketClient more resilient by automatically recognizing a hiccup like that and automatically re-connecting, but until then, you'll need to recognize this yourself.

rmm5t avatar Jan 05 '18 17:01 rmm5t

Hi, this code is working for me, maybe you find it helpful:

// if it closes reconnect
websocket.on('close', (data) => {
    console.log('ERROR', 'Websocket Error', `websocket closed unexpectedly with data: ${data}. Attempting to re-connect.`);

    // try to re-connect the first time...
    websocket.connect();

    let count = 1;
    // attempt to re-connect every 30 seconds.
    // TODO: maybe use an exponential backoff instead
    const interval = setInterval(() => {
        if (!websocket.socket) {
            count++;

            // send me a email if it keeps failing every 30/2 = 15 minutes
            if (count % 30 === 0) {
                const time_since = 30 * count;
                console.log('CRIT', 'Websocket Error', `Attempting to re-connect for the ${count} time. It has been ${time_since} seconds since we lost connection.`);
            }
            websocket.connect();
        }
        else {
            clearInterval(interval);
        }
    }, 30000);
});

wahnker avatar Jan 06 '18 04:01 wahnker

Thanks @rmm5t for clarifying this. Thanks @wahnker for the code. This code assumes that a websocket close event will be fired when the data is not available. Is it always true that if the data is not available its because the websocket is closed? I had incorporated a logic based on latest timestamp received in the heartbeat data. I compare it with the current time, if the difference is more than 15 sec i call websocket.connect()

adityamertia avatar Jan 06 '18 04:01 adityamertia

Honestly, I don't know. I do not use the heartbeat at all. I just look for the close event and attempt to reconnect. So far, it seems to be working pretty reliably, but I'm pretty new to this library and no expert.

wahnker avatar Jan 06 '18 05:01 wahnker

@adityamertia: I reported the same issue in #104. @fb55, feel free to close as duplicate.

@wahnker: that code is almost identical to the code posted by @runia1 in the issue. What's up with that? Also, .on('close') doesn't get a data argument.

tredondo avatar Jan 11 '18 09:01 tredondo

What's up with that?

I'm not sure why you say this. I'm pretty sure I copied the code from that issue and modified it slightly. It is working for me reliably, so I was just trying to share my experience, and maybe help out @adityamertia

keithwahn avatar Jan 11 '18 13:01 keithwahn

HI, yes that code is from me. It's fine though, glad it helped someone. While I'm here I mine as well clarify some stuff:

  1. Yes, on close does not receive data arg, that was my mistake in the original post, and if you try to use it it will just be undefined.
  2. @adityamertia on close should be fired every time the connection is down. Regardless of what causes the loss in connection. This has to do with the way websockets send pings to determine if the socket is still open. HOWEVER that does not guarantee that gdax's servers are never going to have problems... There is a possibility your socket could remain open and their servers could just not be sending you frames. With that said, I've used their websocket api for a while now, and never noticed this kind of behavior. As long as you have a connection frames seem to come down in a timely manner. The code above just ensures that you always have a connection open back up if the one you're using closes.

runia1 avatar Jan 12 '18 21:01 runia1

Has there ever been a time when their servers were not sending frames, and the close event never emitted?

rclai avatar Apr 06 '18 21:04 rclai

Hi, we are closing out PRs + Issues as this project is being archived.

drewrothstein avatar Jan 16 '20 05:01 drewrothstein