-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (65 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const TYPES = ['insert', 'update', 'delete', 'change'];
const CHANNEL_SEPARATOR = '__';
var debug = require('debug')('nuodata-pubsub-api');
var fromChannel = function(channel) {
var channel = channel.split(CHANNEL_SEPARATOR);
var target = channel[1] + '.' + channel[2];
var type = channel[0];
var event = target + ' ' + type;
return {
target: target,
type: type,
event: event,
room: event
};
};
module.exports = function(logger, server, cn) {
var io = require('socket.io')(server);
// instantiate database connection, listen to notification on NEW connection
const pgp = require('pg-promise')({
connect: function (client, dc, isFresh) {
client.on('notification', function (data) {
var info = fromChannel(data.channel);
// broadcast to the room
io.sockets.in(info.room).emit(info.event, data.payload);
});
}
});
const pg = pgp(cn);
io.on('connection', function (socket) {
socket.on('disconnecting', function () {
debug('Disconnecting from socket, trying to remove from channels');
for (var room in socket.rooms) {
}
});
// handle subscribe events
socket.on('subscribe', function (data) {
debug('Subscribing on channel');
data = JSON.parse(data);
if (!TYPES.includes(data.type)) {
socket.emit('error', 'Type can only one of ' + TYPES.join(', '));
}
if (data.target === undefined) {
return socket.emit('error', 'Target cannot be null');
}
pg.connect()
.then(function (db) {
debug('Subscribing on database channel');
db.one('SELECT nuodata_pubsub.subscribe($1, $2);', [data.target, data.type])
.then(function (data) {
var info = fromChannel(data.subscribe);
socket.join(info.room);
// return the channel name
debug('Subscribed to database channel');
socket.emit('subscribed', info);
})
.catch(function (error) {
socket.emit('error', 'Database error, could not subscribe to channel');
});
});
});
socket.on('unsubscribe', function (data) {
});
});
};