I have a โarticlesโ table in a Postgresql 9.1 database and a trigger that notifies the channel of each insert.
I would like to create a node.js script that catches these inserts and issues notifications to connected clients using Socket.io. So far I am using the node-postgres module for LISTEN for the channel, but it seems that the LISTEN request expires in 10-15 seconds and stops capturing inserts. I can request a new listen when the timeout happens, but I'm not sure how to implement the continuation correctly.
Here is my postgresql notification procedure:
CREATE FUNCTION article_insert_notify() RETURNS trigger AS $$ BEGIN NOTIFY "article_watcher"; RETURN NULL; END; $$ LANGUAGE plpgsql;
Trigger:
CREATE TRIGGER article_insert_trigger AFTER INSERT ON article FOR EACH ROW EXECUTE PROCEDURE article_insert_notify();
And node.js code:
var pg = require ('pg'), pgConnection = "postgres://user: pass@localhost /db" pg.connect(pgConnection, function(err, client) { client.query('LISTEN "article_watcher"'); client.on('notification', function(data) { console.log(data.payload); }); });
How can I ensure full time LISTEN or how can I catch these timeouts to listen to the listen request again? Or maybe a module other than node-postgres offers more suitable tools for this?
source share