How to send two variables in one message using Socket.io?

Viewed 37538

I have a clientid and username and i want them both send with the socket.

 client.userid = userid;
 client.username = username;
 client.emit('onconnected', { id: client.userid, name: client.username });

i tried this for example but it doesn't seem to work

3 Answers

You should pass an object to socket event.

In server-side:

socket.emit('your-event', { name: 'Whatever', age: 18 })

In client-side:

socket.on('your-event', ({ name, age }) => {
    // name: Whatever
    // age: 18
})
Related