CaliDog / certstream-js

Javascript library for connecting to the CertStream network.
MIT License
61 stars 9 forks source link

Connection closed by certstream-server due to missing ping packets #7

Open heipei opened 4 years ago

heipei commented 4 years ago

I discovered that using certstream-js (and any other websocket client), the connection by certstream-server is closed if there are no ping/heartbeat packets sent by the client.

certstream-js doesn't send any and also doesn't auto-reconnect so this effectively stop consumption. I didn't want to file this as an issue against certstream-server since I believe certstream-server/cowboy is behaving correctly. I don't have a workaround right now, just posting this for visibility.

scharc commented 4 years ago

Any news on this?

Fitblip commented 4 years ago

Hi there, sorry about the delay. I don't have time to implement this unfortunately, but I'll happily review a PR to add these client pings.

tasinet commented 3 years ago

PR looks good.

You can also fix this in your client script:

const client = new CertStreamClient(function(message){
    console.log(message);
});

client.connect();

setInterval(() => client.ws.ping(), 30000);
RedSpid3r commented 3 years ago

PR looks good.

You can also fix this in your client script:

const client = new CertStreamClient(function(message){
    console.log(message);
});

client.connect();

setInterval(() => client.ws.ping(), 30000);

For me this did not work, I was getting errors:

2021-09-09T13:10:39: Connecting to wss://certstream.calidog.io/...
2021-09-09T13:10:39: Connection established to certstream! Waiting for messages...
2021-09-09T13:16:24: /home/**********/node_modules/ws/lib/WebSocket.js:360
2021-09-09T13:16:24:       throw new Error('not opened');
2021-09-09T13:16:24:       ^
2021-09-09T13:16:24:
2021-09-09T13:16:24: Error: not opened
2021-09-09T13:16:24:     at WebSocket.ping (/home/**********/node_modules/ws/lib/WebSocket.js:313:13)
2021-09-09T13:16:24:     at Timeout._onTimeout (/home/**********/**********/certStream.js:171:42)
2021-09-09T13:16:24:     at listOnTimeout (internal/timers.js:554:17)
2021-09-09T13:16:24:     at processTimers (internal/timers.js:497:7)
tasinet commented 3 years ago

For me this did not work, I was getting errors:


2021-09-09T13:10:39: Connecting to wss://certstream.calidog.io/...
2021-09-09T13:10:39: Connection established to certstream! Waiting for messages...
2021-09-09T13:16:24: /home/**********/node_modules/ws/lib/WebSocket.js:360
2021-09-09T13:16:24:       throw new Error('not opened');

This happens when your websocket has been closed (or it wasn't open, but from your timestamps I can rule that out).

You can see what is happening by registering these event handlers:

client.connect(); // must happen first for client.ws to be created

setInterval(() => client.ws.ping(), 30000);

client.ws.on('terminate',  (...args) => console.log('terminate', ...args));

client.ws.on('close',  (...args) => console.log('close', ...args));

client.ws.on('error', (...args) => console.log('error', ...args) );

when your websocket is closed you need to clear the ping setInterval like this:

// setup like this; setInterval returns a numeric ID identifying the interval
const pingInterval = setInterval(() => client.ws.ping(), 30000);

// later when you want to cancel it:
clearInterval(pingInterval)

and set one up again when you re-connect.

RedSpid3r commented 3 years ago

Thanks for the reply. I added the above handlers and now I can see:

2021-09-10T04:02:29: Connecting to wss://certstream.calidog.io/...
2021-09-10T04:02:29: Connection established to certstream! Waiting for messages...
2021-09-10T07:07:13: close 1006

I see that 1006 is "Abnormal Closure" but I don't see any errors in my logs

ImLunaHey commented 1 year ago

Did anyone resolve this? Using the .ping() method and/or sending a ping packet manually doesn't seem to make a difference with this.

No matter what I get disconnected after ~60s.

gcleaves commented 1 year ago

@ImLunaHey From what I can tell regular disconnects are fairly standard with certstream. If you use the Python command line tool you'll notice that it disconnect and reconnects often.

I think the missing piece with the node library is the automatic reconnection. I faced this by watching the 'close' event and reconnecting.

client.ws.on('close', function() {
    console.log("CLOSE");
    // reconnect logic
});