Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE OR REPLACE FUNCTION notify_app_after_table_insert()
- RETURNS TRIGGER AS
- $BODY$
- BEGIN
- PERFORM pg_notify('devices', json_build_object('old', row_to_json(OLD), 'new', row_to_json(NEW))::text);
- RETURN new;
- END;
- $BODY$
- LANGUAGE plpgsql;
- CREATE TRIGGER trigger_notify_app_after_table_insert
- AFTER UPDATE
- ON devices
- FOR EACH ROW
- EXECUTE PROCEDURE notify_app_after_table_insert();
- // NodeJS
- pool.connect().then((client) => {
- console.log('REGISTERING LISTENERS FOR NOTIFICATIONS')
- client.query('LISTEN devices')
- client.on('notification', (msg) => {
- console.log('MESSAGE', msg)
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement