title: client connecting twice to server, Socket.io
body: <p>I am trying to log username of logged in users by connecting socket.io to passport.js using passport.socketio. It is successful however it logs the username twice and after trying and searching for a fair amount of time I am stuck.</p>
  
  <p>The code sections are as follows:</p>
  
  <p>The Server Code:</p>
  
  <pre><code>var server = http.createServer(app);
  var io = require('socket.io')(server);
  
  io.use(passportSocketIo.authorize({
    cookieParser: cookieParser,       // the same middleware you registred in express
    key:          'connect.sid',       // the name of the cookie where express stores its session_id
    secret:       'hello',    // the session_secret to parse the cookie
    store:        new (require("connect-mongo")(Esession))({
      url: "mongodb://localhost/local"
    }),        // we NEED to use a sessionstore. no memorystore please
    success:      onAuthorizeSuccess,  // *optional* callback on success - read more below
    fail:         onAuthorizeFail,     // *optional* callback on fail/error - read more below
  }));
  
  function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');
    accept(null, true);
    accept();
  }
  
  function onAuthorizeFail(data, message, error, accept){
    if(error)
      throw new Error(message);
    console.log('failed connection to socket.io:', message);
  
    // We use this callback to log all of our failed connections.
    accept(null, false);
  
    // OR
  
    // If you use socket.io@1.X the callback looks different
    // If you don't want to accept the connection
    if(error)
      accept(new Error(message));
    // this error will be sent to the user as a special error-package
    // see: http://socket.io/docs/client-api/#socket &gt; error-object
  }
  io.on('connection', function(socket) {
    var userID = socket.request.user;
    console.log(userID.tg+ " has connected")
  
  });
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  var port = normalizePort(process.env.PORT || '3000');
  app.set('port', port);
  server.listen(port);
  </code></pre>
  
  <p>The Client Code</p>
  
  <pre><code>&lt;script src="http://code.jquery.com/jquery-1.11.1.js"&gt;&lt;/script&gt;
  &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var socket = io.connect();
  &lt;/script&gt;
  </code></pre>
  
  <p>The output is as follows:</p>
  
  <pre><code>successful connection to socket.io
  UserName has connected
  UserName has connected
  </code></pre>
  
  <p>I am unsure as to why it is outputting twice any help would be much appreciated. I am relatively new to Node.js but I don't believe it's the passport part that is causing it, however I am stuck so I might not have the best idea.</p>
  
  <p>Thanks in Advanced</p>
  
  <p>EDIT:
  Tried the latest version of socket and checked my own version both seem to be latest versions, Just to make sure I updated the client code to:</p>
  
  <pre><code>&lt;script src="http://code.jquery.com/jquery-1.11.1.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdn.socket.io/socket.io-1.3.2.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var socket = io.connect();
  &lt;/script&gt;
  </code></pre>
  
  <p>the issue persists</p>