mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-05 05:25:49 +01:00
exposed redis' "connect" method, so socket.io can call it from outside database/redis.js, fixed NodeBB requiring Redis as a socket.io store. It will now go back to using in-memory store per process, if no redis config is found in the NodeBB configuration. re: #2097
This commit is contained in:
@@ -47,26 +47,7 @@
|
|||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
var redis_socket_or_host = nconf.get('redis:host');
|
redisClient = module.connect();
|
||||||
|
|
||||||
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
|
|
||||||
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
|
|
||||||
redisClient = redis.createClient(nconf.get('redis:host'));
|
|
||||||
} else {
|
|
||||||
/* Else, connect over tcp/ip */
|
|
||||||
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nconf.get('redis:password')) {
|
|
||||||
redisClient.auth(nconf.get('redis:password'));
|
|
||||||
} else {
|
|
||||||
winston.warn('You have no redis password setup!');
|
|
||||||
}
|
|
||||||
|
|
||||||
redisClient.on('error', function (err) {
|
|
||||||
winston.error(err.stack);
|
|
||||||
process.exit(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
module.client = redisClient;
|
module.client = redisClient;
|
||||||
|
|
||||||
@@ -104,6 +85,34 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.connect = function() {
|
||||||
|
var redis_socket_or_host = nconf.get('redis:host'),
|
||||||
|
cxn;
|
||||||
|
|
||||||
|
if (!redis) redis = require('redis');
|
||||||
|
|
||||||
|
if (redis_socket_or_host && redis_socket_or_host.indexOf('/') >= 0) {
|
||||||
|
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
|
||||||
|
cxn = redis.createClient(nconf.get('redis:host'));
|
||||||
|
} else {
|
||||||
|
/* Else, connect over tcp/ip */
|
||||||
|
cxn = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nconf.get('redis:password')) {
|
||||||
|
cxn.auth(nconf.get('redis:password'));
|
||||||
|
} else {
|
||||||
|
winston.warn('You have no redis password setup!');
|
||||||
|
}
|
||||||
|
|
||||||
|
cxn.on('error', function (err) {
|
||||||
|
winston.error(err.stack);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
return cxn;
|
||||||
|
};
|
||||||
|
|
||||||
module.close = function() {
|
module.close = function() {
|
||||||
redisClient.quit();
|
redisClient.quit();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -66,23 +66,32 @@ function onUserDisconnect(uid, socketid, socketCount) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Sockets.init = function(server) {
|
Sockets.init = function(server) {
|
||||||
var RedisStore = require('socket.io/lib/stores/redis'),
|
// Default socket.io config
|
||||||
redis = require('redis'),
|
var config = {
|
||||||
pub = redis.createClient(),
|
|
||||||
sub = redis.createClient(),
|
|
||||||
client = redis.createClient();
|
|
||||||
|
|
||||||
io = socketioWildcard(SocketIO).listen(server, {
|
|
||||||
log: false,
|
log: false,
|
||||||
transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'],
|
transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'],
|
||||||
'browser client minification': true,
|
'browser client minification': true,
|
||||||
resource: nconf.get('relative_path') + '/socket.io',
|
resource: nconf.get('relative_path') + '/socket.io'
|
||||||
'store' : new RedisStore({
|
};
|
||||||
|
|
||||||
|
// If a redis server is configured, use it as a socket.io store, otherwise, fall back to in-memory store
|
||||||
|
if (nconf.get('redis')) {
|
||||||
|
var RedisStore = require('socket.io/lib/stores/redis'),
|
||||||
|
database = require('../database/redis'),
|
||||||
|
pub = database.connect(),
|
||||||
|
sub = database.connect(),
|
||||||
|
client = database.connect();
|
||||||
|
|
||||||
|
config.store = new RedisStore({
|
||||||
redisPub : pub,
|
redisPub : pub,
|
||||||
redisSub : sub,
|
redisSub : sub,
|
||||||
redisClient : client
|
redisClient : client
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
} else if (nconf.get('cluster')) {
|
||||||
|
winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.')
|
||||||
|
}
|
||||||
|
|
||||||
|
io = socketioWildcard(SocketIO).listen(server, config);
|
||||||
|
|
||||||
Sockets.server = io;
|
Sockets.server = io;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user