11

I am currently building an application using node.js and using the socket.io module. When a user connects I am storing data specific to the user against their socket. For example

io.sockets.on('connection', function (socket) {

    socket.on('sendmessage', function (data, type) {

        socket.variable1 = 'some value';
        socket.variable2 = 'Another value';
        socket.variable3 = 'Yet another value';
    });
});

While this works my question is, is this a good way to do it. I am effectively storing session data but is there a better way to do it?

2 Answers 2

4

I think that you should store those variables in another type of object. Keep the socket object only for the communication. You may generate an unique id for every user and create a map. Something like this:

var map = {},
numOfUsers = 0;

io.sockets.on('connection', function (socket) {
    numOfUsers += 1;
    var user = map["user" + numOfUsers] = {};
    socket.on('sendmessage', function (data, type) {
        user.variable1 = 'some value';
        user.variable2 = 'Another value';
        user.variable3 = 'Yet another value';
    });
});
4
  • Thanks, your way is better. Coming from a PHP background I keep forgetting there is no "refresh" on the backend so you can store data like that and not worry about losing it.
    – Pattle
    Commented Aug 19, 2013 at 10:02
  • Yep, you may use some additional module for generating unique ids for every user. Using a simple iterator like numOfUsers is a little bit ugly.
    – Krasimir
    Commented Aug 19, 2013 at 10:09
  • 3
    +1 And its probably not a bad idea to also store user.socket = socket so that you can identify a user just by knowing its socket. Commented Aug 19, 2013 at 12:07
  • 1
    It's easier to use the built-in socket.id as a key to your data structure rather than inventing your own.
    – Pete Alvin
    Commented Apr 22, 2016 at 9:25
1

Update: io.set() and io.get() methods are deprecated

A reasonable way is to choose a data store and associate each data with a unique socket identifier (id, for example).


A recommended way is to use the native socket.set and socket.get to set and get data asynchronously specifically to the current socket.

Following your example:

io.sockets.on('connection', function (socket) {

    socket.on('sendmessage', function (data, type) {

        socket.set('variable1', 'some value');
        socket.set('variable2', 'Another value');
        socket.set('variable3', 'Yet another value');
    });
});

Also, you can call a function asynchronously after you set a value:

...
socket.set('variable1', 'some value', function () {
    /* something to be done after "variable1" is set */
});
...

Finally, you can retrieve a variable doing:

...
var variable1 = socket.get('variable1')
...

Or use it directly when needed:

if ( socket.get('age') > 30 ) {
    // Vida longa às eleições presidenciais diretas no Brasil
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.