question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LISTEN notifications stop after a while

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
panklekscommented, Mar 3, 2017

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.

    listen(fn: (message: { channel: string, payload: string }, client: Pg.Client) => void, ...channels: string[]) {
        let
            client = new Pg.Client(this._config);

        client.connect((err) => {
            if (err) {
                console.error(`Can't connect listener.`);

                setTimeout(() => {
                    this.listen(fn, ...channels);
                }, 1000 * LISTEN_RECONNECT_S);

                return;
            }

            client.on("notification", (message) => {
                fn(message, client);
            });

            client.on("error", (err) => {
                console.error(`Lost connection for listener.`);
                this.listen(fn, ...channels);
            });

            for (let e of channels)
                client.query(`LISTEN ${e}`);
        });
    }
3reactions
vitaly-tcommented, Mar 16, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found