title: Reconnect socket on user login
body: <p>I work with a setup created by create-react-app and use flux for data management and the application needs to implement socket on the client side (I use socket.io for this purpose).</p>
  
  <p>Currently the socket is initialised in a Socket.js file the following way:</p>
  
  <pre><code>import io from 'socket.io-client';
  import { remoteUrl } from './constants/RemoteUrl';
  import SocketWorker from './utilities/SocketWorker';
  
  let socket = io.connect(remoteUrl + '?role=user');
  
  socket.on('statusChange', (data) =&gt; {
    return SocketWorker.receiveOrderStatusChange(data);
  })
  
  export  { socket };
  </code></pre>
  
  <p><strong>It does work, however the problem is that it only tries to connect to the server once, when the site is loaded. When the user opens the site unauthenticated it does not connect and misses to reconnect, thus the connection is not established and socket events are not received</strong></p>
  
  <p>I have tried to create a class instead and react an API for reconnect on the object, like:</p>
  
  <pre><code>import io from 'socket.io-client';
  import { remoteUrl } from './constants/RemoteUrl';
  import SocketWorker from './utilities/SocketWorker';
  
  function Socket() {
    this.socket = io.connect(remoteUrl + '?role=user');
    this.reconnect = () =&gt; {
      this.socket = io.connect(remoteUrl + '?role=user');
    }
  }
  
  let socket = new Socket();
  
  socket.socket.on('statusChange', (data) =&gt; {
    return SocketWorker.receiveOrderStatusChange(data);
  })
  
  export { socket };
  </code></pre>
  
  <p>I tried to call the Socket.reconnect() method, although it did not work and connection was not established either. Any idea or alternative solution?</p>