Last active
June 22, 2017 00:07
-
-
Save mastfissh/83038468706aa59a62e5a4b6db84ee77 to your computer and use it in GitHub Desktop.
Super lo-fi pub/sub
This file contains 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
-- In publisher: | |
Name: notify_trigger(); Type: FUNCTION; Schema: public; Owner: - | |
CREATE FUNCTION notify_trigger() RETURNS trigger | |
LANGUAGE plpgsql | |
AS $$ | |
DECLARE | |
BEGIN | |
PERFORM pg_notify('orders', TG_TABLE_NAME || ',id,' || row_to_json(NEW)::text ); | |
RETURN new; | |
END; | |
$$; | |
CREATE TRIGGER watched_table_trigger AFTER INSERT ON orders FOR EACH ROW EXECUTE PROCEDURE notify_trigger(); | |
--- In subscriber: | |
var pg = require ('pg'); | |
var pgConString = process.env.COMMERCE_HOST | |
pg.connect(pgConString, function(err, client) { | |
if(err) { | |
console.log(err); | |
} | |
client.on('notification', function(msg) { | |
console.log(msg); | |
}); | |
var query = client.query("LISTEN orders"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment