javascript - Disconnect from Redis cleanly when Node exits? - Stack Overflow

Currently I have the following code, working with Node.js, socket.io and Redis:var io = require('s

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

You 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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347695a4426880.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信