title: Duplicates using Node/Express/Socket.IO/Angular
body: <p>My client is dropping its first socket.io message and seeing duplicates for other messages.  The server sends one message at a time but the client misses the first message, receives the second message once, the third message twice and the fourth three times, etc..   On the server side I have Node server using Express.  On the client I am using Angular.  Versions: Socket.IO:0.9.16, Node:v0.10.26, Express:4.4.4, Angular:1.2.18.  Server: Linux RH5.  Client: WinXP/Chrome 36.0.1985.97 beta-m.  Help appreciated in advance.</p>
  
  <hr>
  
  <p>Server: test.js</p>
  
  <pre><code>var express = require('express')
    , app = express()
    , server = require('http').createServer(app)
    , io = require('socket.io').listen(server);
  
  server.listen(3011);
  io.set('log level', 5);
  app.use(express.static(__dirname+"/public"));
  
  var seqNum = 1;
  var connects = 0;
  io.sockets.on('connection', function(socket) {
     console.log("Connected");
     connects =+ 1;
     socket.emit('status',{ status: "starting" });
     socket.emit('status',{ status: "running" });
  });
  
  setInterval(function() {
     if (connects &gt; 0) {
        console.log("Sending: "+seqNum);
        io.sockets.emit('test', seqNum);
        seqNum++;
     }
     else
        console.log("No one yet");
  }, 2000);
  </code></pre>
  
  <hr>
  
  <p>Client: public/index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;script src="socket.io/socket.io.js"&gt;&lt;/script&gt;
    &lt;script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"&gt;&lt;/script&gt;
    &lt;script src="/app.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body ng-app="TestApp"&gt;
     &lt;div ng-controller="MainCtrl" border=1&gt;
        &lt;h4&gt;Msgs&lt;/h4&gt;
        &lt;span&gt;Status: {{connStatus}}&lt;/span&gt;
        &lt;ul&gt;
          &lt;li ng-repeat="msg in messages track by $index"&gt;{{$index + 1}}: {{msg}}&lt;/li&gt;
        &lt;/ul&gt;
     &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <p>client: public/app.js</p>
  
  <pre><code> var app = angular.module("TestApp",[]);
  
    // We define a factory the socket service is instantiated only once, and
    // thus act as a singleton for the scope of the application
    app.factory("socket", function ($rootScope) {
      console.log("Load io factory");
      var socket = io.connect();
      return {
        on: function (eventName, callback) {
          socket.on(eventName, function () {
            var args = arguments;
            socket.on(eventName, callback);
          });
        },
        emit: function (eventName, data, callback) {
          socket.emit(eventName, data, function () {
            var args = arguments;
            $rootScope.$apply(function () {
              if (callback) {
                callback.apply(socket, args);
              }
            });
          })
        }
      };
    });
  
   function MainCtrl($scope, socket) {
      console.log("Setting stuff");
      $scope.messages = new Array;
      $scope.connStatus = "Off";
  
      socket.on('connect', function() {
         console.log("Connected");
         $scope.connStatus = "On";
         $scope.$apply();
      });
  
      socket.on('status', function(msg_in) {
         console.log("Status: "+ msg_in.status);
         $scope.connStatus = msg_in.status;
         $scope.$apply();
      });
  
      socket.on('test', function(msg_in) {
         console.log("Recvd: "+msg_in);
         $scope.messages.push(msg_in);
         $scope.$apply();
      });
  
      socket.on('error', function() {
         console.log("Error on socket");
         $scope.connStatus = $scope.connStatus + " - error";
      });
   }
  </code></pre>
  
  <hr>
  
  <p>Server console:</p>
  
  <p>node test.js</p>
  
  <pre><code>   info  - socket.io started
  No one yet
  No one yet
     debug - served static content /socket.io.js
     debug - client authorized
     info  - handshake authorized y2eaJREuSjqvp4PthfG2
     debug - setting request GET /socket.io/1/websocket/y2eaJREuSjqvp4PthfG2
     debug - set heartbeat interval for client y2eaJREuSjqvp4PthfG2
     debug - client authorized for
     debug - websocket writing 1::
  Connected
     debug - websocket writing 5:::{"name":"status","args":[{"status":"starting"}]}
     debug - websocket writing 5:::{"name":"status","args":[{"status":"running"}]}
  Sending: 1
     debug - websocket writing 5:::{"name":"test","args":[1]}
  Sending: 2
     debug - websocket writing 5:::{"name":"test","args":[2]}
  Sending: 3
     debug - websocket writing 5:::{"name":"test","args":[3]}
  Sending: 4
     debug - websocket writing 5:::{"name":"test","args":[4]}
  Sending: 5
     debug - websocket writing 5:::{"name":"test","args":[5]}
  Sending: 6
     debug - websocket writing 5:::{"name":"test","args":[6]}
  Sending: 7
     debug - websocket writing 5:::{"name":"test","args":[7]}
  </code></pre>
  
  <hr>
  
  <p>Client console: (note the number before the line shows how many times it has been received.  The console collapses duplicate lines) </p>
  
  <pre><code>Load io factory
  Setting stuff
  Status: running
    Recvd: 2
  2 Recvd: 3
  3 Recvd: 4
  4 Recvd: 5
  5 Recvd: 6
  6 Recvd: 7
  </code></pre>
  
  <hr>
  
  <p>Client window:</p>
  
  <pre><code>Msgs
  
  Status: running
  1: 2
  2: 3
  3: 3
  4: 4
  5: 4
  6: 4
  7: 5
  8: 5
  9: 5
  10: 5
  11: 6
  12: 6
  13: 6
  14: 6
  15: 6
  16: 7
  17: 7
  18: 7
  19: 7
  20: 7
  21: 7
  </code></pre>