Send additional data on socket connection


Question

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.

1
66
7/22/2017 10:18:50 PM

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();
});
52
10/12/2014 9:30:25 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon