I am working on a complex game with Nodejs and Socket.io, where I need to store socket.io objects in the memory and also assign properties to the socket object ( say a name , a counter of some action from the socket , etc )
In the code below, I have shown an example of what I am trying to achieve. I store all the sockets in an array and also have another array which stores the name property of the socket. At any time if I get a request for the name, I can just pick the name from the array in the memory.
But now I have too many users and I need to load-balance my application across multiple servers. So I cant store objects and properties in the memory. I need to store them in a Database.
I am planning to use Redis. This link tells how to use Redis Store for sockets -
https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO
But how do I associate my other properties ( say name etc ) to the socket object in the Redis Store ? If there is some new ways to achieve this , please let me know also.
var socket_array = new Array();
var socket_name_array = new Array();
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket_array.push(socket);
var i = socket_array.indexOf(socket);
var name = generate_random_name();
socket_name_array[i]= name;
socket.on('get_name', function (data) {
var i = socket_array.indexOf(socket);
var name= socket_name_array[i]
socket.emit('socket_name' , {name :name } );
});
});
function generate_random_name(){
var random_string;
//code
return random_string;
}