forked from facebookarchive/Realtime-Demo
-
Notifications
You must be signed in to change notification settings - Fork 38
/
subscriptions.js
55 lines (46 loc) · 1.73 KB
/
subscriptions.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
var redis = require('redis'),
fs = require('fs'),
jade = require('jade'),
io = require('socket.io'),
settings = require('./settings'),
helpers = require('./helpers'),
app = settings.app,
subscriptionPattern = 'channel:*',
socket = io.listen(app);
// We use Redis's pattern subscribe command to listen for signals
// notifying us of new updates.
var redisClient = redis.createClient(settings.REDIS_PORT, settings.REDIS_HOST);
var pubSubClient = redis.createClient(settings.REDIS_PORT, settings.REDIS_HOST);
pubSubClient.psubscribe(subscriptionPattern);
pubSubClient.on('pmessage', function(pattern, channel, message){
helpers.debug("Handling pmessage: " + message);
/* Every time we receive a message, we check to see if it matches
the subscription pattern. If it does, then go ahead and parse it. */
if(pattern == subscriptionPattern){
try {
var data = JSON.parse(message)['data'];
// Channel name is really just a 'humanized' version of a slug
// san-francisco turns into san francisco. Nothing fancy, just
// works.
var channelName = channel.split(':')[1].replace(/-/g, ' ');
} catch (e) {
return;
}
// Store individual media JSON for retrieval by homepage later
for(index in data){
var media = data[index];
media.meta = {};
media.meta.location = channelName;
redisClient.lpush('media:objects', JSON.stringify(media));
}
// Send out whole update to the listeners
var update = {
'type': 'newMedia',
'media': data,
'channelName': channelName
};
for(sessionId in socket.clients){
socket.clients[sessionId].send(JSON.stringify(update));
}
}
});