How to reconnect to socket.io namespace after disconnect - node.js

I connect to a namespace like
var socket = io.connect('http://localhost:3000/random');
then i disconnect like this
socket.disconnect();
my question is how do I reconnect to the 'random' namespace using the socket again?
I tried using
socket.socket.connect('http://localhost:3000/random');
not working

Connect using
var socket = io.connect('http://localhost:3000/random');
Cleanly disconnect using
socket.socket.disconnect();
Reconnect using
socket.socket.reconnect();

Related

socket.io-client web socket connection failing in node.js child process

I am using simple socket.io client module to connect to web socket but the connection is failing. The way I have learned is that right after you define socket, you can access connected property to find out the status of connection and it always return false. I am trying to connect to web socket in a child process on the same server where my main process is running.
var socket = require('socket.io-client')("ws://xx.xx.xxx.xxx");
console.log(socket.connected);
SocketIO connections should be initiated over HTTP(S):
var socket = require('socket.io-client')('http://localhost:6001');
(instead of ws://...)
Then wait for the connect event; when that fires, the client is connected to the server;
socket.on('connect', function() {
console.log('connected to server!');
...
});

socket.io client side in nodejs app

i am using socket.io for communication between server and client.
My client side is not a html. My client side is javascript file. so here is my code of client side
var io = require('socket.io-client')
var socket = io.connect('http://localhost:3000/home');
socket.on('connect', function () {
console.log(' Connected!');
});
On server side i have received the connection event but on client side connect event doesn't fire. I have tested through html way, it works but why its not working through a java script file.
I have no idea what you mean with tested through html way, but try socket.on("connection", ...),
not "connect".
could be related to the fact that you're specifying a namespace different from the default.
try to handle the connection at http://localhost:3000, instead of http://localhost:3000/home
var socket = io.connect('http://localhost:3000');
found a similar situation here

Are sockets reused on reconnect event?

A socket is created when a user connects to the server.
The connection is broken because of interwebz malfunction.
Client reconnects automatically.
Will the socket connection be the same as when he first connected? Or will the socket be a new one?
Thanks!
A new connection is established on reconnect. It can be checked by printing the socket.id.
On server:
var io = require('socket.io)(server);
io.on('connection', function (socket) {
console.log('socket id is ' + socket.id); }
Excerpt from socket.io documentation
The property is present once the socket has connected, is removed when the socket disconnects and is updated if the socket reconnects.
I'd also like to preserve state following reconnects in an electron app (e.g. if the host gets suspended then resumed, socket.io gets disconnected).
I haven't tried this yet, but the following article discusses how to reuse socket id's across reconnects. I'll update if I have any success:
https://newbedev.com/reuse-socket-id-on-reconnect-socket-io-node-js

nodejs socket.io : How to reconnect socker after socket.disconnect() is called?

How can I connect to socket again after I call disconnect
Here is the client source code
socket = io.connect('http://myServer:1339'); // connect socket
socket.disconnect(); // disconnect socket
socket = io.connect('http://myServer:1339'); // does not connect socket again !
Why do I call disconnect ?
Because I have myServer and myServer2 servers. If I switch between myserver and myserver2, I got many sockets connections (like adding)
Any idea on how to clearly close a socket and open a new one ?
Regards
You should not call io.connect a second time.
To reconnect, you should use
socket.socket.connect()

Socket.IO is not triggering the connect event

I'm trying to setup a Socket.IO server, and right now, connecting doesn't seem to be working the way it does in the Wiki. I'm connecting to a server using the namespace /client, and I'm successfully seeing the connection made in the debug log, but the connection message is never display (and the other contents are never getting attached).
Server-side
var clients = io
.of('/client')
.on('connect', function (socket) {
// This is never getting run
console.log('Client connected');
});
Client side
var socket = io.connect('http://localhost:8082/client');
Why, in the above code, am I not getting the 'Client connected' message in my console?
So, turns out this is a very simple issue, as I suspected. The client uses the connect event, but the server uses connection.
The code should look like:
var clients = io
.of('/client')
.on('connection', function (socket) {
// This is never getting run
console.log('Client connected');
});

Resources