Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra params with socket.io

How can I send extra parameters with the connection in socket.io? So when a client connects, they send additional information, and server-side it is received as

io.on('connection', function(client, param1, param2, param3) {
    // app code
}
like image 490
JRPete Avatar asked Jun 08 '11 20:06

JRPete


People also ask

How much traffic can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Is Socket.IO scalable?

"The Socket.IO server currently has some problems with scaling up to more than 10K simultaneous client connections when using multiple processes and the Redis store, and the client has some issues that can cause it to open multiple connections to the same server, or not know that its connection has been severed."

How many messages per second can Socket.IO handle?

Load benchmarks Here we can see that the HTTP benchmark peaks at about~950 requests per second while Socket.io serves about ~3900 requests per second.

Which is better Socket.IO or pusher?

Both Pusher Channels and socket.io allow you to publish and subscribe to messages in realtime using the WebSocket protocol. The main difference is that Channels is a hosted service/API, and with socket.io you need to manage the deployment yourself.


1 Answers

Here's a little trick which should work. First, you create your own Socket client that sends a message on first connection (containing all your additional info).

// Client side

io.MySocket = function(your_info, host, options){
  io.Socket.apply(this, [host, options]);
  this.on('connect', function(){
    this.send({__special:your_info});
  });
};
io.util.inherit(io.MySocket, io.Socket);

var my_info = {a:"ping"}
var socket  = io.MySocket(my_info);

Then on the server side, you modify your socket to listen for the special message and fire off an event when it does.

// Server side

var io = require('socket.io').listen(app);
io.on('connection', function(client){
  client.on('message', function(message){
    if (message.__special) {
      io.emit('myconnection', client, message.__special);
    }
  });
});

io.on('myconnection', function(client, my_info) {
  // Do your thing here
  client.on('message', ...); // etc etc
}

This is the technique I used to link my session handling into Socket.IO for a package I wrote.

like image 103
Dave Avatar answered Sep 20 '22 20:09

Dave