@GoodCodeHQ - by Deni Bertovic / @denibertovic
NOTIFY channel [ , payload ]
pg_notify(text, text)
LISTEN channel;
UNLISTEN channel;
function initPostgresPubSub(socket) {
socket.on('subscribe', function(channel) {
socket.join(channel);
console.log("New socket joined...");
if (!listening.hasOwnProperty(channel)) {
// only create new connection and new listeners for new
// channels
var client = new pg.Client(pgConnectionString);
client.connect();
client.query('LISTEN "'+channel+'"');
listening[channel] = client;
client.on('notification', function(data) {
try {
var obj = JSON.parse(data.payload);
io.sockets.in(channel).emit('notification', obj);
} catch(e) {
console.log(e);
console.log(data.payload);
}
});
}
});
}
'startSocket': function() {
var self = this;
this.socket = io.connect(this.options.socketServer, {
transports: ['websocket', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
});
this.socket.on('connect', function() {
// here we subscribe to the unique channel for notifications only for this particular Admin
self.socket.emit('subscribe', 'watchers_' + self.options.SOME_UNIQUE_ID );
});
this.socket.on('notification', function(notification) {
var update = new APP.Model.Update(notification);
self.options.updates.splice(0, 0, update);
console.log('New update: ', arguments);
});
},
CREATE OR REPLACE FUNCTION notify_my_table_insert() RETURNS trigger AS $$
DECLARE
BEGIN
PERFORM pg_notify('watchers_' || NEW.id, row_to_json(NEW)::TEXT);
RETURN new;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS my_insert_trigger ON my_table;
CREATE TRIGGER my_insert_trigger AFTER INSERT ON my_table
FOR EACH ROW EXECUTE PROCEDURE notify_my_table_insert();
That we didn't have to do any Python.