Send additional data on socket connection

node.jssocket.io

node.js Problem Overview


How to best send additional data upon socket connection?

Client:

socket.on('connect',function(){ 
//I'd like set some values and pass them up to the node server.
});

Node.js Server:

io.on('connection', function(client){
//I'd like to get the data here.
});

For example sending a user name or email address etc.

node.js Solutions


Solution 1 - node.js

You should send your data either in connect or on create:

var s = io('http://216.157.91.131:8080/', { query: "foo=bar" });
s.connect();

var c = io.connect('http://216.157.91.131:8080/', { query: "foo=bar" });

With the new version of socket.io, on server is where the things have been changed:

var io = require('socket.io')(server);

io.use(function(socket, next) {
  var handshakeData = socket.request;
  console.log("middleware:", handshakeData._query['foo']);
  next();
});

Solution 2 - node.js

I have a different approach - emit an event right after connecting, with the data:

socket.on('connect',function(){ 
    // Send ehlo event right after connect:
    socket.emit('ehlo', data);
});

io.on('connection', function(client){
    // Receive ehlo event with data:
    client.on('ehlo', function(data) {
    });
});

You can hold a variable/object, and say, when there is no ehlo event with data, the other events are ignored until ehlo is sent.

If this does not satisfy, you can send data right when connecting, I won't copy the code but it is in this answer: https://stackoverflow.com/a/13940399/1948292

Solution 3 - node.js

The connection events get fired as soon as the TCP connection is established. There's no way to send anything in between.

What you can do is to simply take the first message send by the server and put the data in that message. I'd strongly suggest to roll some thin protocol for this, so you would have multiple types of messages and use those to determine how the code should process the data.

This would be more extendable too, since it's fairly easy to come up with a generic architecture for that. If you want to wait for the userdata to come in first you can simply add some state to your connections.

Solution 4 - node.js

this my code for sending query data to nodejs and server.io server client

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

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

query...user1

Solution 5 - node.js

this is my code on socket version 2.1.1

//client side
var socket = io.connect(window.location.origin,{query:'loggeduser=user1'});

// server side
io.socket.on('connection', function (socket) {
    console.log("loggeduser => " +  socket.handshake.query.loggeduser);
});

Solution 6 - node.js

I found the answer located here really helpful in this regard: https://stackoverflow.com/questions/13745519/send-custom-data-along-with-handshakedata-in-socket-io

Also brushing up on the following documentation helps explain ways to tie variables to the handshake object which can be accessed from the socket object during the connection: https://github.com/LearnBoost/socket.io/wiki/Authorizing#global-authorization

Solution 7 - node.js

I'm assuming you will use SSL.

Using querystrings to send sensitive data is not a good security practice (why?). I think that if you need to send non-sensitive data, then Miquel answer is the fastest and more easy one, but if sensitive data is what you need to send, then something like Daniel W. is the best approach to take.

For an authentication approach, you can take a look at this github project (socketio-auth) which, beyond the fact that it is currently with no maintenance, can give you a good idea of how to deal with the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanView Question on Stackoverflow
Solution 1 - node.jsMiquelView Answer on Stackoverflow
Solution 2 - node.jsDaniel W.View Answer on Stackoverflow
Solution 3 - node.jsIvo WetzelView Answer on Stackoverflow
Solution 4 - node.jsmukutView Answer on Stackoverflow
Solution 5 - node.jsManoj RanaView Answer on Stackoverflow
Solution 6 - node.jsArt GeigelView Answer on Stackoverflow
Solution 7 - node.jsoriukenView Answer on Stackoverflow