-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketio_server.js
More file actions
88 lines (75 loc) · 2.99 KB
/
Copy pathsocketio_server.js
File metadata and controls
88 lines (75 loc) · 2.99 KB
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
84
85
86
87
88
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var amqp = require('amqplib');
var when = require('when');
app.get('/', function(req, rsp) {
rsp.sendfile('index.html');
});
app.get('/index.js', function(req, rsp) {
rsp.sendfile('index.js');
});
var jobsQueueName = 'sockbit_work',
announceQueueName = 'sockbit_announce',
jobsChannel;
var prepRabbitAnnounceChannel = function(conn) {
return conn.createChannel().then(function(channel) {
channel.assertExchange(announceQueueName, 'fanout', {durable: false}).then(function() {
return channel.assertQueue('', {exclusive: true});
}).then(function(queueOk) {
announceQueue = queueOk.queue;
return channel.bindQueue(announceQueue, announceQueueName, '');
});
return channel;
});
};
var forwardJob = function(jobName, socket) {
console.log('registering forward of ' + jobName + ' job requests to rabbit');
socket.on(jobName, function(message) {
var jobString = JSON.stringify([jobName, message]);
console.log('forwarding job to rabbit: ' + jobString);
console.log('sending job to rabbit');
jobsChannel.sendToQueue(jobsQueueName, new Buffer(jobString), {deliveryMode:true});
});
};
amqp.connect('amqp://localhost').then(function(conn) {
when(conn.createChannel()).then(function(ch) {
jobsChannel = ch;
return ch.assertQueue(jobsQueueName, {durable: true});
}).then(function() {
return conn.createChannel();
}).then(function(announceChannel) {
announceChannel.assertExchange(announceQueueName, 'fanout', {durable: false});
return announceChannel;
}).then(function(announceChannel) {
var queueOk = announceChannel.assertQueue('', {exclusive: true});
return [announceChannel, queueOk];
}).then(function(objs) {
var announceChannel = objs[0];
announceQueue = objs[1].queue;
announceChannel.bindQueue(announceQueue, announceQueueName, '');
return [announceChannel, announceQueue];
}).then(function(objs) {
var announceChannel = objs[0];
var announceQueue = objs[1];
announceChannel.consume(announceQueue, function(message) {
var update = JSON.parse(message.content);
var announcementName = update[0];
var data = update[1];
console.log('receiving announcement from rabbit: ' + message.content);
console.log('sending update to browser clients');
io.emit(announcementName, data);
}, {noack: true});
}).then(function() {
console.log('listening for announcements from rabbit');
io.on('connection', function(socket) {
console.log('a user connected');
forwardJob('update_note', socket);
forwardJob('get_notes', socket);
});
});
});
var port = process.argv[2];
http.listen(port, function() {
console.log('listening on port ' + port);
});