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.