LISTEN notifications stop after a while
See original GitHub issueI’m using LISTEN
to be notified of changes. It used to work perfectly when I was listening to a DB on localhost
. However, now that I’ve moved the DB over to another VM, I’m encountering some problems where my client loses “sync” with the DB and stops receiving notifications after some time (in order of minutes/hours).
I’m not using the connection pool, here’s how I connect and listen to updates:
var client = new pg.Client(pgConString);
client.connect(function(err, client) {
if(err) {
throw err;
}
client.on('notification', function(msg) {
console.log(msg.payload);
});
client.on('error', function(error) {
console.error('This never even runs:', error);
});
var query = client.query("LISTEN watchers");
});
Is it possible keep this listener working indefinitely? Am I doing something silly? What is supposed to happen after a momentary network failure? Do I have to resign to polling the DB and diffing changes?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:7
- Comments:19 (8 by maintainers)
Top Results From Across the Web
Silence Smartphone Notifications from Interrupting Your Music
When you're listening to music or a podcast on your phone, it's annoying to be interrupted by an incoming text or email notification....
Read more >The sound is lowered when a notification comes, how could I ...
When the notifications come, 1) if you do not enable "Pause for interruptions" feature in the app Settings, the sound will be lowered,...
Read more >3 ways to stop iOS notifications from interrupting music playback
If you prefer an a la carte approach so that some notifications still get through while others are halted, head to Settings >...
Read more >Control notifications on Android - Google Support
After you swipe down from the top of your screen, drag the notification slightly right or left. Then tap Settings . · Tap...
Read more >Headphone notifications on your iPhone, iPod touch, or Apple ...
Listening to audio on your headphones too loud for too long can damage ... After receiving a notification, the next time you plug...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found problem with my case - in fact stupid mistake. Connection used for notification was taken from pool, when I created dedication connection with
new Client(...)
is looks good.Here is piece of TS code that could be reused - hope it helps. In case connection is lost, it will try to reconnect immediately, if this fails, it will try again after some time.
One of the main reasons why this library moved mostly into the use of the connection pool is because it is very awkward dealing with dropped connections, while the pool can maintain connections automatically.
But with the event listening it is impossible to use the pool, because each connection in the pool has an expiration on it.
If you cannot detect a dropped connection through the client interface, you can try and detect it otherwise, and once detected, re-establish the watcher.
If you suspect a networking failure, you can listen for that, somehow.
Like I said, static connections are awkward. And listeners are used by very few people, so there is no standard solution to this, it seems. I would suggest to inquire about this on Stack Overflow.