LISTEN query timeout with node-postgres?

后端 未结 2 1591
星月不相逢
星月不相逢 2021-02-15 11:20

I have an \"article\" table on a Postgresql 9.1 database and a trigger that notifies a channel on each insert.

I\'d like to create a node.js script that catches those in

2条回答
  •  情深已故
    2021-02-15 11:44

    I got answer to my issue on the node-postgres repo. To quote Brianc:

    pg.connect is use to create pooled connections. Using a connection pool connection for listen events really isn't supported or a good idea though. [...] To 'listen' a connection by definition must stay open permanently. For a connection to stay open permanently it can never be returned to the connection pool.

    The correct way to listen in this case is to use a standalone client:

    var pg = require ('pg'),
        pgConnectionString = "postgres://user:pass@localhost/db";
    
    var client = new pg.Client(pgConnectionString);
    client.connect();
    client.query('LISTEN "article_watcher"');
    client.on('notification', function(data) {
        console.log(data.payload);
    });
    

提交回复
热议问题