Created
May 22, 2014 20:52
-
-
Save dorfire/3ec749a6acce106a3484 to your computer and use it in GitHub Desktop.
Node.js PostgreSQL notification server
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
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