Currently I have the following code, working with Node.js, socket.io and Redis:
var io = require('socket.io'), redis = require("redis"), client = redis.createClient();
io.sockets.on('connection', function (socket) {
var socket_id = socket.id;
socket.on('chat', function(data) {
client.set('user:' + socket_id, data['colour']);
// The user left the page. Remove them from Redis.
socket.on('disconnect', function () {
client.del('user:' + socket_id);
client.quit();
});
});
});
This works fine for normal socket connections and disconnections, but there seems to be a problem if Node goes down, or if I just restart Node as part of normal development.
The key is never deleted from Redis. So the number of entries stored in the Redis database gradually grows and grows. I'm also not sure whether the Redis client exists cleanly.
How can I clean up Redis entries and quit the Redis client when Node exits, as well as when the socket disconnects?
Currently I have the following code, working with Node.js, socket.io and Redis:
var io = require('socket.io'), redis = require("redis"), client = redis.createClient();
io.sockets.on('connection', function (socket) {
var socket_id = socket.id;
socket.on('chat', function(data) {
client.set('user:' + socket_id, data['colour']);
// The user left the page. Remove them from Redis.
socket.on('disconnect', function () {
client.del('user:' + socket_id);
client.quit();
});
});
});
This works fine for normal socket connections and disconnections, but there seems to be a problem if Node goes down, or if I just restart Node as part of normal development.
The key is never deleted from Redis. So the number of entries stored in the Redis database gradually grows and grows. I'm also not sure whether the Redis client exists cleanly.
How can I clean up Redis entries and quit the Redis client when Node exits, as well as when the socket disconnects?
Share Improve this question asked Oct 31, 2011 at 19:19 RichardRichard 33k30 gold badges111 silver badges146 bronze badges3 Answers
Reset to default 4You could handle this when node exits, but e.g. in case the power goes down, there's no way you can clean it up at shutdown time. I'd wipe old stuff from the DB at startup time instead.
I've run in to this problem too. My solution was to just use a specific Redis database on the server for Socket.io data. A simple FLUSHDB to that database on start up will clean out stuck keys.
var socketIoDatabase = 4;
var client = redis.createClient();
client.select(socketIoDatabase);
client.flushdb();
Of course if you have multiple node processes using this same database clearing it will cause problems. In this case you can do it during a maintenance window or something while all node processes are terminated.
check this out: http://nodejs/docs/v0.4.12/api/process.html#event_uncaughtException_
var io = require('socket.io'), redis = require("redis"), client = redis.createClient();
io.sockets.on('connection', function (socket) {
var socket_id = socket.id;
socket.on('chat', function(data) {
client.set('user:' + socket_id, data['colour']);
// The user left the page. Remove them from Redis.
socket.on('disconnect', function () {
client.del('user:' + socket_id);
client.quit();
});
});
});
// this will be activated on any error without try catch :)
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});