Created
February 6, 2019 16:49
-
-
Save renatoargh/6793bd272f1782b2bc59b40a0fea4178 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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