Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disconnecting and manually reconnecting Socket.IO

I'm initiating a socket connection using Socket.IO to a Node.js server and it's as simple as follows:

var socket = io.connect(url, options);

At some point, I need to disconnect this socket when it's no longer need and reconnect it. I'm having some issues initiating a completely new socket and I thought it would be much better to simply reconnect the same socket rather then rebuilding another socket and adding listeners to it.

Based on the socket.io documentation, I should be able to manually reconnect a socket by calling:

socket.connect();

But when I call that method, I'm getting an error in the console: Uncaught TypeError: Object # has no method 'connect'

Am I missing anything?

like image 219
Elie Avatar asked Nov 29 '25 16:11

Elie


1 Answers

You should call the socket in your socket object :

var socket = io.connect(url, options);
socket.socket.connect();

See this question it is related : Socket IO reconnect?

like image 193
drinchev Avatar answered Dec 02 '25 07:12

drinchev