0

Currently I am storing the actual socket object in an array in each process. I then store socket data in redis for all processes (workers) to access.

Is there a more performance/efficient way to store and retrieve sockets, or is this pretty much how this is done? I am concerned about performance when the sockets array potentially grows to over 200k objects.

var _ = require('underscore');

//Storing the socket in memory and saving the redis associated data
var server = this;
//This function adds this hash to redis and returns a unique id
server.gs.addSocketInfo({
     "userid": 0,
     "displayName": "Mike's iPhone",
     "connecttime": new Date().getTime()
  }, function(err, socketid) {
     socket.socketid = socketid;
     server.sockets.push(socket);
     doReady();
  });

//To retrieve the data and socket later
//function returns the socket data from redis
server.gs.getSocketInfo(socket.socketid, function(err, sinfo) {
    var socket = _.find(server.sockets, function(socket) {
       return socket.socketid === sinfo.socketid;   
    });
    if (socket)
       //Do something with the socket here...
});
Mikt25
  • 695
  • 1
  • 9
  • 18
  • The architecture of your solution is not clear from the question. Why are you putting socket data into Redis? – jimbo Jun 20 '13 at 18:13
  • Some data about the socket such as the userid needs to be in redis for inter-process communication (using cluster). I obviously didn't add all of the code for the example. Is there anything I could add to make it more clear? – Mikt25 Jun 20 '13 at 18:22
  • The fact that you're using a cluster is important. Are these regular old TCP sockets? or something higher level (ex: HTTP)? Are all of the processes in your system under your control, or are some of the systems clients which you don't control? – jimbo Jun 20 '13 at 18:25
  • Actually I probably should mention this. I have a net server and a http server using socket.io. The socket objects in that array could be either an http socket or a tcp socket depending on what server was connected to. All systems are under my control. PS: I made a change to the underscore retrieve. – Mikt25 Jun 20 '13 at 18:29

0 Answers0