6

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

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Ludo, Hi, this works fine for me, but when I am using different namespaces, the session data does not persist between namespaces, like suppose I have a namespace A where I set socket.username and I have a namespace B where I want to access this socket.username. How do I do this? Is there a way to persist the session data between namespaces?
3

Like what Ludo said, set the nickname property directly in the socket:

// get the name of the sender
var name = socket.nickname;
console.log('Chat message by ', name);
console.log('error ', err);
sender = name;

and

// this kind of emit will send to all! :D
socket.nickname = name;
io.sockets.emit('chat', {
  msg : "Welcome, " + name + '!', 
  msgr : "Nickname"
});

1 Comment

@Iyen, Hi, this works fine for me, but when I am using different namespaces, the session data does not persist between namespaces, like suppose I have a namespace A where I set socket.username and I have a namespace B where I want to access this socket.username. How do I do this? Is there a way to persist the session data between namespaces?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.