Open tremby opened 5 years ago
Related, I'm surprised there isn't any event for when the connection is successful. Or at least I don't see one. Or is the third argument to twat.stream
only fired once a connection is successfully made?
One more thing: when I added the code to run twat.stream
again on "destroy", this is what I saw in the log next time it happened.
20:55:04.956 ram-twitterbot logplex <190>1 2019-07-19T03:55:01.494382+00:00 host app bot.1 - Twitter stream: 'end' received. Expecting to also see a 'destroy' event, after which we'll attempt to reconnect.
20:55:04.956 ram-twitterbot logplex <190>1 2019-07-19T03:55:01.495420+00:00 host app bot.1 - Twitter stream: destroy: the socket has been destroyed
20:55:04.956 ram-twitterbot logplex <190>1 2019-07-19T03:55:01.495561+00:00 host app bot.1 - Twitter stream: attempt to reconnect...
20:55:04.956 ram-twitterbot logplex <190>1 2019-07-19T03:55:01.498673+00:00 host app bot.1 - Twitter stream: connected? This is the handler of twat.stream running
20:55:04.956 ram-twitterbot logplex <190>1 2019-07-19T03:55:01.743708+00:00 host app bot.1 - Twitter stream: reconnect: Network Error undefined
... but later it does respond (twice) to incoming tweets.
On the reconnect event, the two things it's logging are info.error
and info.attempts
, which it's spitting out as "Network Error" and undefined.
So I haven't used this in years. I do remember when writing this, for an early attempt at autoreconnect, I tried something similar to your approach, and ran into a similar problem. If you create a new connection (twat.stream), it generates a new event handler. In order to prevent duplicated events, you have to remove the old event listener.
I think I tried encapsulating some of that logic here https://github.com/mileszim/twat/blob/master/lib/stream.js#L148-L167
This was written way before the ES2015/2017/2019 standards with classes and all that. If I were to write this today, I think the event handling logic would be a lot cleaner with explicit classes and management.
Somehow you'll need to make a "singleton" kind of class with the ability to track event listeners (instances of twat.stream I think), and remove them when disconnected.
I was running this for a while and at some point in the logs I see an "end" event came in, followed by a "destroy". I'm not 100% certain, but I don't think it reconnected afterwards. At least, no "reconnect" event was fired.
So I adjusted my code a little, so on the "destroy" event it would run
twat.stream(...)
again.I waited, and eventually saw the "end" and "destroy" come in again, and my log saying it would reconnect. But since then when receiving tweets the handler is being fired twice.
What's the correct way to handle these events?
I want to always try to reconnect (with backoff as necessary), and I always want the same handlers on the events.
Thanks.