Advertisement
metalni

Untitled

Apr 2nd, 2024 (edited)
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CREATE OR REPLACE FUNCTION notify_app_after_table_insert()
  2. RETURNS TRIGGER AS
  3. $BODY$
  4.     BEGIN
  5.         PERFORM pg_notify('devices', json_build_object('old', row_to_json(OLD), 'new', row_to_json(NEW))::text);
  6.         RETURN new;
  7.     END;
  8. $BODY$
  9. LANGUAGE plpgsql;
  10.  
  11. CREATE TRIGGER trigger_notify_app_after_table_insert
  12. AFTER UPDATE
  13. ON devices
  14. FOR EACH ROW
  15. EXECUTE PROCEDURE notify_app_after_table_insert();
  16.  
  17.  
  18. // NodeJS
  19. pool.connect().then((client) => {
  20.           console.log('REGISTERING LISTENERS FOR NOTIFICATIONS')
  21.           client.query('LISTEN devices')
  22.           client.on('notification', (msg) => {
  23.             console.log('MESSAGE', msg)
  24.           })
  25.         })
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement