-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenvironment.js
127 lines (102 loc) · 4.28 KB
/
environment.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
module.exports = {
loadSocketIo: function loadSocketIo(redis) {
var port = process.env.PORT || 5001;
if (process.env.NODE_ENV != 'production') {
port = 5001; // run on a different port when in non-production mode.
}
console.log('STARTING ON PORT: ' + port);
var io = require('socket.io').listen(Number(port));
io.on('connection', function(socket) {
socket.on('realtime_user_id_connected', function(message) {
console.log('Realtime User ID connected: ' + message.userId);
});
socket.on('disconnect',function(){
redis.sub.removeListener('message', onMessage);
});
redis.sub.on('message', onMessage);
function onMessage(channel, message){
// can't deliver a message to a socket with no handshake(session) established
if (socket.request === undefined) {
return;
}
msg = JSON.parse(message);
var currentSocketIoUserId = socket.request.session['user_id'];
// if the recipient user id list is not part of the message
// then define it anyways.
if (msg.recipient_user_ids === undefined || msg.recipient_user_ids == null) {
msg.recipient_user_ids = [];
}
if (msg.recipient_user_ids.indexOf(currentSocketIoUserId) != -1) {
delete msg.recipient_user_ids; //don't include this with the message
socket.emit('realtime_msg', msg);
}
};
});
return io;
},
authorize: function authorize(io, redis) {
io.use(function(socket, next) {
var sessionId = null;
var userId = null;
var url = require('url');
requestUrl = url.parse(socket.request.url);
requestQuery = requestUrl.query;
requestParams = requestQuery.split('&');
params = {};
for (i=0; i<=requestParams.length; i++){
param = requestParams[i];
if (param){
var p=param.split('=');
if (p.length != 2) { continue };
params[p[0]] = p[1];
}
}
sessionId = params["_rtToken"];
userId = params["_rtUserId"];
// retrieve session from redis using the unique key stored in cookies
redis.getSet.hget([("rtSession-" + userId), sessionId],
function(err, session) {
if (err || !session) {
next(new Error('Unauthorized Realtime user (session)'));
} else {
socket.request.session = JSON.parse(session);
next();
}
}
);
});
},
loadRedis: function loadRedis() {
var redis = require('redis');
var url = require('url');
var redisURL = url.parse("redis://127.0.0.1:6379/0");
var redisSub, redisPub, redisGetSet = null;
if (process.env.REDISCLOUD_URL == null) {
// use local client if there's no redis cloud url set up.
redisSub = redis.createClient();
redisPub = redis.createClient();
redisGetSet = redis.createClient();
} else {
// use environment redis connection info.
redisURL = url.parse(process.env.REDISCLOUD_URL);
redisSub = redis.createClient(redisURL.port, redisURL.hostname, {
no_ready_check: true
});
redisPub = redis.createClient(redisURL.port, redisURL.hostname, {
no_ready_check: true
});
redisGetSet = redis.createClient(redisURL.port, redisURL.hostname, {
no_ready_check: true
});
redisSub.auth(redisURL.auth.split(":")[1]);
redisPub.auth(redisURL.auth.split(":")[1]);
redisGetSet.auth(redisURL.auth.split(":")[1]);
}
redisSub.subscribe('realtime_msg');
return {
pub: redisPub,
sub: redisSub,
getSet: redisGetSet,
};
},
}