title: Socket.io opening multiple connections with React-Native
body: <p>I built a server with an API. It uses Axios for the un-logged calls and Socket.io for the logged calls.
  Then I have a website connected to it. And that works perfectly.
  But I have also an application built in react-native which has a strange behavior: it opens connections on every emit without closing them previous connections. As you can see below, I console.log the websocket.engine.clientsCount on the server. Every time I emit from the phone-application it opens a new connection, finding the server with an increasing number of it.</p>
  
  <p><a href="https://i.stack.imgur.com/I6mBG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I6mBG.png" alt="enter image description here"></a></p>
  
  <p>On server, I user following versions:</p>
  
  <pre><code>"connect-mongo": "^1.3.2",
  "express": "^4.14.1",
  "express-session": "^1.12.1",
  "jwt-simple": "^0.5.1",
  "mongodb": "^2.2.30",
  "mongoose": "^4.11.5",
  "passport": "^0.3.2",
  "passport-jwt": "^2.2.1",
  "passport-local": "^1.0.0",
  "socket.io": "^1.7.3",
  "socketio-jwt": "^4.5.0"
  </code></pre>
  
  <p>Here the code of the API. I removed some code for clarity.</p>
  
  <pre><code>const passport = require('passport');
  const express = require('express');
  const session = require('express-session');
  const http = require('http');
  const morgan = require('morgan');
  const mongoose = require('mongoose');
  const socketio = require('socket.io');
  const bodyParser = require('body-parser');
  const socketioJwt   = require("socketio-jwt"); // da commentare
  const Users = require('../models/users');
  
  const passportService = require('./services/passport');
  
  const requireAuth = passport.authenticate('jwt', {session: false});
  const requireLogin = passport.authenticate('local', {session: false});
  const config = require('./config');
  
  const app = express();
  const socketRouter = require('./services/socketRouter');
  const MongoStore = require('connect-mongo')(session);
  
  const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost/blablabla';
  mongoose.connect(mongoUri);
  ...
  const server = http.Server(app);
  const websocket = socketio(server);
  
  // add authorization for jwt-passport when first connection -&gt; https://github.com/auth0/socketio-jwt
  websocket.use(socketioJwt.authorize({
    secret: config.secret,
    handshake: true
  }));
  
  const sessionMiddleware = session({
    store: new MongoStore({ // use MongoDb to store session (re-using previous connection)
      mongooseConnection: mongoose.connection,
      ttl: (1 * 60 * 60)
    }),
    secret: config.secretSession,
    httpOnly: true,
    resave: false,
    saveUninitialized: false,
    cookie: { maxAge: 86400000 }
  });
  app.use(sessionMiddleware);
  ...
  websocket.on('connection', (socket) =&gt; {
      Users.findById(socket.decoded_token.sub, function(err, user) {
          if (err) { console.log('the user wasn\'t find in database', err); }
          if (user) {
              socket.join(user._id);
              console.log('Clients connected: ', websocket.engine.clientsCount);
  
              // ------ PROTECTED EVENTS ------ //
              ...
              // ------------------------------ //
  
          }
  
          socket.on('disconnect', ()=&gt; {
            socket.leave(user._id);
            onsole.log('user disconnected');
          });
      });
  });
  ...
  </code></pre>
  
  <p>I won't put the initialisation of the website because it works good.</p>
  
  <p>On mobile application, I user following versions:</p>
  
  <pre><code>"react-native": "^0.41.0",
  "react-native-keychain": "^1.1.0",
  "socket.io-client": "^1.7.3",
  "socketio-jwt": "^4.5.0"
  </code></pre>
  
  <p>Here is the innitialisation of the react-native application.</p>
  
  <pre><code>import * as Keychain from 'react-native-keychain';
  import { BASIC_WS_URL } from '../api';
  
  const io = require('socket.io-client/dist/socket.io');
  const socketEvents = require('./events');
  
  exports = module.exports = (store) =&gt; {
    Keychain.getGenericPassword().then((credentials) =&gt; {
      if (credentials &amp;&amp; credentials !== false) {
        const { password } = credentials;
        const websocket = io(BASIC_WS_URL, {
          jsonp: false,
          transports: ['websocket'], // you need to explicitly tell it to use websockets
          query: {
            token: password
          }
        });
  
        websocket.connect();
        websocket.on('connect', (socket) =&gt; {
          console.log('Connected');
        });
  
        websocket.on('reconnect', (socket) =&gt; {
          console.log('Re-connected');
        });
  
        websocket.on('disconnect', (socket) =&gt; {
          console.log('Disconnected');
        });
        // all the events to listen
        socketEvents(websocket, store);
      }
    });
  };
  </code></pre>
  
  <p>What al I doing wrong?</p>