socket.get() AND socket.set() gives problems

node.js, sockets

Solution

You have to set the nickname property directly in the socket!

From socket.io website:

The old io.set() and io.get() methods are deprecated and only supported for backwards compatibility. Here is a translation of an old authorization example into middleware-style.

Also, from an example of the socket.io website :

// usernames which are currently connected to the chat
var usernames = {};
var numUsers = 0;

    // when the client emits 'add user', this listens and executes
  socket.on('add user', function (username) {
    // we store the username in the socket session for this client
    socket.username = username;
    // add the client's username to the global list
    usernames[username] = username;
    ++numUsers;
    addedUser = true;
    socket.emit('login', {
      numUsers: numUsers
    });
    // echo globally (all clients) that a person has connected
    socket.broadcast.emit('user joined', {
      username: socket.username,
      numUsers: numUsers
    });
  });

Check the example here : https://github.com/Automattic/socket.io/blob/master/examples/chat/index.js

Problem

I'm using NodeJs v0.10.28 and everytime i try to login to the chat i get a error `TypeError: Object #<Socket> has no method 'set' at Socket.<anonymous>` And if i'm deleting the lines it works but not working right. What is the wrong thing in this code ``` // get the name of the sender socket.get('nickname', function (err, name) { console.log('Chat message by ', name); console.log('error ', err); sender = name; }); ``` AND ``` socket.set('nickname', name, function () { // this kind of emit will send to all! :D io.sockets.emit('chat', { msg : "Welcome, " + name + '!', msgr : "Nickname" }); }); ``` FULL CODE http://pastebin.com/vJx7MYfE

Original source