Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
newpanjing opened this issue Dec 30, 2020 · 5 comments
Closed

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

newpanjing opened this issue Dec 30, 2020 · 5 comments
Labels
question Further information is requested

Comments

@newpanjing
Copy link

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');
});
@tannerkrewson
Copy link

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

@darrachequesne
Copy link
Member

@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", () => {});

@darrachequesne darrachequesne added the question Further information is requested label Jan 2, 2021
@tannerkrewson
Copy link

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. 😄

@joezappie
Copy link

I too agree this is kind of confusing. I thought I could listen for reconnect directly on the socket and not socket.io. Would be nice if there was a warning if you try to use an unknown event for socket like @tannerkrewson suggested. Looking back at the documentation though, I clearly didn't read. There are multiple stackoverflows that suggest reconnect works directly on the socket object.

darrachequesne added a commit to socketio/socket.io-website that referenced this issue Feb 11, 2021
@darrachequesne
Copy link
Member

I'm not sure that adding a warning is a good idea, because users can actually use the "reconnect" event.

I've added a note in the documentation, to make it clearer: https://socket.io/docs/v3/client-socket-instance/#Events

I'm going to close this for now, please reopen if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants