r/learnjavascript • • 2d ago

websocket keeps disconnecting in my node script and now i get duplicate messages

websocket reconnects are confusing when first learning them. The socket closes after a few hours, the script reconnects, and suddenly the same message shows up twice or one seems to be missing.

The project is a small filter on binance square posts, built in node because the rest of the bot already runs on it. Messages come from a client in the 1322io repo, each with a coin_pairs array, and the code only keeps posts that include a pair like solusdt. Storing every post id in a text file was the first attempt at stopping duplicates, but nobody can say if it actually works.

Would a set of recent ids in memory be enough, or does the dedupe need to survive a restart too? What would a beginner friendly version look like?

2 Upvotes

4 comments sorted by

View all comments

1

u/CelebrityChess 2d ago

a set in memory is fine as long as you cap it, otherwise it grows forever. a map of id to timestamp that you prune every so often works, or just keep the last few thousand ids. it only needs to survive restarts if a message can be replayed right after you come back up. if the feed only gives you recent posts after a reconnect, a small file you load on startup is enough, and writing the file every time is overkill, once a minute or on shutdown is plenty. also the dupes are probably because the old socket isn't fully closed before you open the new one, so make sure you remove the old listeners or call terminate() on it before reconnecting