Skip to content

Instantly share code, notes, and snippets.

@dorfire
Created May 22, 2014 20:52
Show Gist options
  • Save dorfire/3ec749a6acce106a3484 to your computer and use it in GitHub Desktop.
Save dorfire/3ec749a6acce106a3484 to your computer and use it in GitHub Desktop.
Node.js PostgreSQL notification server
const SOCKETIO_LISTEN_PORT = 81;
const PG_CONNECTION_STRING = 'postgres://dor@localhost/app';
const PG_NOTIFICATION_CHANNEL = 'notifications';
var pg = require('pg'),
sio = require('socket.io').listen(SOCKETIO_LISTEN_PORT);
var db = new pg.Client(PG_CONNECTION_STRING);
db.connect();
sio.sockets.on('connection', function(socket)
{
socket.on('join', socket.join);
});
db.on('notification', function(data)
{
var model = JSON.parse(data.payload);
var room = 'event_'+ model.event_pk;
console.log('emitting notification #%d to room "%s"', model.pk, room);
sio.sockets.in(room).emit('notification', model);
});
db.query('LISTEN ' + PG_NOTIFICATION_CHANNEL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment