socket.set and socket.get

2,068 views
Skip to first unread message

rsdrsd

unread,
Apr 12, 2013, 9:15:53 AM4/12/13
to sock...@googlegroups.com
When I do something like this on server side:
 
var chatbox = io.on('connection',function(socket) {
  socket.set('id','woooooooow');
  socket.on('disconnect',function() {
    var id = socket.get('id');
  }
}
 
It gives me some errors and node won't start. Does someone knows why this is? When I use the callbacks in get it works.

Артем Деуля

unread,
Apr 12, 2013, 9:57:26 AM4/12/13
to sock...@googlegroups.com
1) check for socket object availability. 
3) Maybe you did not set default storage


2013/4/12 rsdrsd <rdo...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Socket.IO" group.
To unsubscribe from this group and stop receiving emails from it, send an email to socket_io+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

rsdrsd

unread,
Apr 12, 2013, 12:24:12 PM4/12/13
to sock...@googlegroups.com
No I don't mean that. It is working, but only when I use a callback in the socket.get method. When I do it like var id = socket.get('id') I can't start the app, because it gives a lot of errors. When I do something like: socket.get('id',function(err,id){}); I can retrieve the value of id. I need to have these values in the socket.on('disconnect',function(){}).
 

utan

unread,
Apr 13, 2013, 1:00:35 AM4/13/13
to Socket.IO
I think you don't need the function get to retrieve the socket id
disconnecting, just call it socket.id and you have it right there...

regards

rsdrsd

unread,
Apr 13, 2013, 1:34:01 AM4/13/13
to sock...@googlegroups.com
I just want to set and get a variable on the socket. Don't want to retrieve the socket.id.

utan

unread,
Apr 13, 2013, 12:20:58 PM4/13/13
to Socket.IO
I don't know, because they work asynchronously you may have to set it
with method set and retrieve its value
socket.manager.sockets[uid].store and use the key you used to set the
object..

I don't use socket.io internal methods.. I've created my own objects..

its just an idea. regards

Sergio Canales

unread,
Apr 17, 2013, 1:28:56 PM4/17/13
to sock...@googlegroups.com
The thing is that socket.get and socket.set are asynchronous, that's why you must use a callback; still, you could easily get the value you need on socket.on('disconnect', function(){}); like below, just remember to always use callbacks for setting and getting, since both operations are asynchronous and otherwise you risk the value not being set when you are getting it. Hope this helps.


var chatbox = io.on('connection',function(socket) {
// Use callback for setting the value as well
 socket.set('id','woooooooow', function(){
     //at this point you can be sure the value has been properly set
    socket.on('disconnect',function() {
        socket.get('id', function(error, value){
           //the function returns an error and a value variable, check for errors and if none are present, then the value of 'id' should be in the "value" variable
           var id = value;     // 'woooooooow'

        });
    });
 });
});
Reply all
Reply to author
Forward
0 new messages