title: Node.js Socket.io running slow
body: <p>My node.js application uses TCP and Socket.io connections. It forwards data from TCP to relevant socket.io connections.</p>
  <p>What I've noticed is, there is a minor delay while sending data over WebSockets (socket.io), not so significant during minimal payloads, but as the number of payloads increases the delay becomes significant.</p>
  <p>Is this a known issue? A single publish on socket.io takes about 1.5secs.</p>
  <p>Here's my code.</p>
  <pre><code>var net = require('net');
  
  var httpServer = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(8080);
  console.log('Server running at http://127.0.0.1:1337/');
  
  // The Socket.io jibber-jabber
  var io = require('socket.io').listen(httpServer);
  io.sockets.on('connection', function (socket) {
    socket.emit('con',&quot;Connected!&quot;);
    socket.on('subscribe', function(data) { socket.join(data.room); });
    socket.on('unsubscribe', function(data) { socket.leave(data.room); });
    webSockets.push(socket);
    console.log(&quot;Websocket connected.&quot;);
    
    socket.on('disconnect', function () {
      var index = webSockets.indexOf(socket);
    webSockets = webSockets.splice(index,1);
    console.log(&quot;Websocket disconnected.&quot;);
    });
  });
  
  function SendToSocket(room, mssg)
  {
      console.log(&quot;--- Sending too rooms ---&quot;);
      var time1 = new Date();
      io.sockets.in(room).emit(room, mssg);
      var time2 = new Date();
      console.log(time1.getSeconds(),time1.getMilliseconds(),&quot;---&quot;,time2.getSeconds(),time2.getMilliseconds());
  }
  
  var shortServer = net.createServer(function(c) { //'connection' listener
  console.log('TCP client connected');
    c.on('end', function() {
  console.log('TCP client disconnected');
  c.destroy();
    });
  
    c.on('data', function(data){
  console.log(data);
              SendToSocket('domain',data);
    });
  });
  
  
  shortServer.listen(2501, function() { //'listening' listener
    console.log('TCP Server bound');
  });
  </code></pre>
  <p>The output is as follows</p>
  <pre><code>   info  - socket.io started
  TCP Server bound
  TCP client connected
  &lt;Buffer ff fb 1f ff fb 20 ff fb 18 ff fb 27 ff fd 01 ff fb 03 ff fd 03&gt;
  --- Sending too rooms ---
  42 564 '---' 44 245
  </code></pre>
  <p>42 567 indicates seconds and microseconds respectively before the socket.io operation
  while 44 245 indicates the time after the operation. A lag of close to 2 secs.</p>
  <p>The <code>sendToSocket</code> function is called whenever there is data on any TCP connection. I've timed the function and it takes close to 1.5secs to go through. It's the same if I don't use rooms, and use a simple for loop to send data to all socket connections.</p>
  <p>More Details:
  Max message payload from TCP - 50 bytes.
  Max message payload to be sent on socket.io - 256 bytes OR chars.
  Frequency - as high as 3 times a second.
  No: of connections - Max 10.</p>
  <p>P.S: During the test, I noticed, even if I send a blank message from just one connection on TCP with no Socket.io connections; socket.io holds the app for 1.5secs during the call of <code>SendToSocket</code></p>