Skip to content

Instantly share code, notes, and snippets.

@renatoargh
Created February 6, 2019 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoargh/6793bd272f1782b2bc59b40a0fea4178 to your computer and use it in GitHub Desktop.
Save renatoargh/6793bd272f1782b2bc59b40a0fea4178 to your computer and use it in GitHub Desktop.
// CREATE TABLE realtime_table (id serial primary key, title varchar, year varchar, producer varchar);
// CREATE FUNCTION notify_trigger() RETURNS trigger AS $$
// DECLARE
// BEGIN
// PERFORM pg_notify('watch_realtime_table', row_to_json(NEW)::text);
// RETURN new;
// END;
// $$ LANGUAGE plpgsql;
// CREATE TRIGGER watch_realtime_table_trigger AFTER INSERT ON realtime_table
// FOR EACH ROW EXECUTE PROCEDURE notify_trigger();
// INSERT INTO realtime_table (title, year, producer) VALUES ('Renato', 1987, 'Works!')
const pg = require ('pg');
const pool = new pg.Pool({
connectionString: '',
})
async function main() {
pool.connect(async (err, client) => {
if(err) {
throw err
}
client.on('notification', function(msg) {
console.log(JSON.stringify(JSON.parse(msg.payload), null, 4))
});
console.log('Listening...')
await client.query('LISTEN watch_realtime_table');
});
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment