node.js - Need help to verify if this socket.io code will work on multiple server instances -


imagine there node.js chatting app. going hosted on aws scale across multiple instances. chat server app has piece of codes tracks online or offline using variable called "sockets". code works on single instance. code fail when server scaled across multiple instances?

var sockets = {};  io.configure(function () {      ... });  io.sockets.on('connection', function(socket) {     socket.on('online', function(data){         if(data.userid){             sockets[userid] = socket;             console.log("user " + userid + " connected.");         }     });      socket.on('disconnect', function () {         for(var key in sockets){             if(sockets[key] == socket)             {                 delete sockets[key];                  console.log("user " + key + " disconnected.");                 break;             }         }     }); });      // check if userid connected // still work when multiple server instances function isuseridonline(userid) {     return (sockets[userid] != null); } 

no, not work multiple instances. sockets represent users connected particular server, not other servers.

the answer question involves storing connected , server connected in central database server socket server can find out if connected , server connected contacting central database. since data temporal, in-memory database redis used.

a realated discussion of various architectural options using multiple servers handle connections while solving "who online problem , how contact them" in answer.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -