Categories
JavaScript Answers

How to send custom data along with handshake data in Node socket.io?

Spread the love

To send custom data along with handshake data in Node socket.io, we get the handshake data from the socket.manager.handshaken property.

For instance, we write

const socket = io.connect(window.location.origin, {
  query: "loggeduser=user1",
});

io.sockets.on("connection", (socket) => {
  const endp = socket.manager.handshaken[socket.id].address;
  console.log(socket.manager.handshaken[socket.id].query.user);
});

to call connect to make the connection.

Then we listen for the connection event with on.

In the callback, we get the handshake data with socket.manager.handshaken

And we get the query string from the query property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.