Socket.io-client: Socket.io - Client 3.0.0+ reconnect event all failed

Created on 30 Dec 2020  ·  3Comments  ·  Source: socketio/socket.io-client

Socket.io - Client 3.0.0+ reconnect event all failed.

Switch to version 2.3.1 and it works.

const io = require('socket.io-client');

let socket = io.connect('ws://127.0.0.1:8000', {
    "transports": ['websocket', 'polling'],
    reconnection: true
});
socket.on('connect', function(data){
    onlineFlag = true;
    console.log(data + ' - connect');
});
socket.on('connect_error', function(data){
    console.log(data + ' - connect_error');
});
socket.on('connect_timeout', function(data){
    console.log(data + ' - connect_timeout');
});
socket.on('error', function(data){
    console.log(data + ' - error');
});
socket.on('disconnect', function(data){
    onlineFlag = false;
    console.log(data + ' - disconnect');
});
socket.on('reconnect', function(data){
    console.log(data + ' - reconnect');
});
socket.on('reconnect_attempt', function(data){
    console.log(data + ' - reconnect_attempt');
});
socket.on('reconnecting', function(data){
    console.log(data + ' - reconnecting');
});
socket.on('reconnect_error', function(data){
    console.log(data + ' - reconnect_error');
});
socket.on('reconnect_failed', function(data){
    console.log(data + ' - reconnect_failed');
});
socket.on('ping', function(data){
    console.log(data + ' - ping');
});
socket.on('pong', function(data){
    console.log(data + ' - pong');
});
question

Most helpful comment

@newpanjing hi! What do you mean by "reconnect event all failed" ?

As pointed out by @tannerkrewson (thanks!), the Socket instance no longer emits any reconnection events.

// no longer emitted
socket.on("reconnect_attempt", () => {});
// but you can listen to the events emitted by the Manager instance (the io attribute)
socket.io.on("reconnect_attempt", () => {});

All 3 comments

I had this issue too; I figured it out!! It's by design:

https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html#The-Socket-instance-will-no-longer-forward-the-events-emitted-by-its-Manager

@newpanjing hi! What do you mean by "reconnect event all failed" ?

As pointed out by @tannerkrewson (thanks!), the Socket instance no longer emits any reconnection events.

// no longer emitted
socket.on("reconnect_attempt", () => {});
// but you can listen to the events emitted by the Manager instance (the io attribute)
socket.io.on("reconnect_attempt", () => {});

Would it be possible to have a warning message in the console if someone tries to use those events on the socket itself? I think that would be super helpful for people migrating, instead of it silently failing. 😄

Was this page helpful?
0 / 5 - 0 ratings