Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucaswxp/fc12fb1b7d9011fb20ec to your computer and use it in GitHub Desktop.
Save lucaswxp/fc12fb1b7d9011fb20ec to your computer and use it in GitHub Desktop.
socket.io force new connection not working fix

Socket.io connected the first time, but after disconnected, I couldn't connect again manually.

For me, the flags 'force new connection' and 'forceNew' wasn't working properly like some places says it should, and I couldn't find any way around it. Using socket.io 1.3.5, this is what I did to fix it:

var once = false;

function getSocket(){
  var socket = io({path: '/socket.io'}, {'force new connection':true});
  
  // this part here does the trick
  if(once){
  	socket.connected = false;
  	socket.connect();
  	socket.removeAllListeners();
  }
  once = true;
  
  return socket;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment