Skip to content

Instantly share code, notes, and snippets.

@dorfire
Created May 22, 2014 20:52
Show Gist options
  • Select an option

  • Save dorfire/3ec749a6acce106a3484 to your computer and use it in GitHub Desktop.

Select an option

Save dorfire/3ec749a6acce106a3484 to your computer and use it in GitHub Desktop.

Revisions

  1. dorfire created this gist May 22, 2014.
    26 changes: 26 additions & 0 deletions notification_server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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);