Skip to content

Instantly share code, notes, and snippets.

@mastfissh
Last active June 22, 2017 00:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastfissh/83038468706aa59a62e5a4b6db84ee77 to your computer and use it in GitHub Desktop.
Save mastfissh/83038468706aa59a62e5a4b6db84ee77 to your computer and use it in GitHub Desktop.
Super lo-fi pub/sub
-- 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