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 stops working randomly until page refresh #5299

Open
vitortoigo opened this issue Feb 18, 2025 · 8 comments
Open

Socket.IO stops working randomly until page refresh #5299

vitortoigo opened this issue Feb 18, 2025 · 8 comments

Comments

@vitortoigo
Copy link

Hi, I'm using Socket.IO in a Node.js project, and sometimes the socket connection just stops working. The only way to get it working again is by refreshing the browser page.

I'm using namespaces to separate tenants, and Redis to manage socket IDs. Here is my frontend and backend configuration:

Frontend (Client-side):

export function connectSocketToNamespace(namespace: string): void {
    socket = io(`${import.meta.env.VITE_SERVER_URL}/${namespace}`, {
        auth: {
            token: getAuthToken()
        },
        query: {
            sessionKey: getSession()
        },
        transports: ["websocket"],
        upgrade: false,
        reconnection: true,
        reconnectionAttempts: Infinity,
        reconnectionDelay: 2000,
        reconnectionDelayMax: 10000,
        randomizationFactor: 0.5,
    });
}

Backend (Server-side - AdonisJS):

public boot() {
    if (!this.booted) {
        this.io = new Server(AdonisServer.instance!, {
            maxHttpBufferSize: maxUploadFileSize(),
            pingInterval: 10000,
            pingTimeout: 25000,
            cors: {
                origin: Env.get('CLIENT_URL')
            }
        });

        this.booted = true;

        this.configureNamespaces();
        this.clearOldSockets();
    }
}

There are no errors in the console or server logs, and this issue happens randomly.

Has anyone experienced something similar? Could this be related to the Redis adapter or the namespace handling? Any suggestions on debugging or fixing this issue?

Thanks in advance!

@vitortoigo vitortoigo added the to triage Waiting to be triaged by a member of the team label Feb 18, 2025
@darrachequesne
Copy link
Member

Hi! That's really weird indeed, I guess you aren't able to reproduce it reliably? Does it happen in a given browser? Or after a specific delay? How do you detect it?

@darrachequesne darrachequesne added needs investigation and removed to triage Waiting to be triaged by a member of the team labels Feb 20, 2025
@vitortoigo
Copy link
Author

Hi! That's really weird indeed, I guess you aren't able to reproduce it reliably? Does it happen in a given browser? Or after a specific delay? How do you detect it?

Hi! Yeah, I can't reproduce the error consistently. It happens in any browser, and I believe it occurs after a certain amount of time. My application is a chat, so I'm sending messages normally, but at some point, I try to send a new message, and it just doesn't go through. The only way to make it work again is by refreshing the page.

@darrachequesne
Copy link
Member

Without the ability to reproduce the issue, it will be quite hard to find the cause... We'd need to have access to the internal state of the client when that happens.

Could it simply be a temporary disconnection? Or a missing event listener?

@ivosluganovic
Copy link

ivosluganovic commented Feb 27, 2025

I believe I might have the same problem -- I initially the issue was a result of updating python-socketio, but it seems that this started happening after updating socket.io-client on the client side from 4.7.5 to 4.8.1.

When the socket.io-client tries establishing a connection, the Python server detects a successful connection, but there is an immediate disconnect.
On the other side, the client does not retry beyond the 3 times, as you can see in the logs of the original issue, and even states "connected: true".

You can see the details here: miguelgrinberg/python-socketio#1434

After downgrading to 4.7.5, the issue seems to be gone.

@mhasnain306
Copy link

mhasnain306 commented Mar 7, 2025

I was facing the same issue, it turned out that I was using nodemon, and whenever I was changing something in the server project, the server was restarting so the socket ids were changing on every server restart and by not refreshing the browser I was not registering the new ids. So after change in the server code refreshing the browser was only fix.

@vitortoigo
Copy link
Author

I believe I might have the same problem -- I initially the issue was a result of updating python-socketio, but it seems that this started happening after updating socket.io-client on the client side from 4.7.5 to 4.8.1.

When the socket.io-client tries establishing a connection, the Python server detects a successful connection, but there is an immediate disconnect. On the other side, the client does not retry beyond the 3 times, as you can see in the logs of the original issue, and even states "connected: true".

You can see the details here: miguelgrinberg/python-socketio#1434

After downgrading to 4.7.5, the issue seems to be gone.

Thank you for the response! I will try to do this downgrade.

@ivosluganovic
Copy link

Just to give more info: the downgrade did not help at all , and we are still facing the same issue.

@ivosluganovic
Copy link

@darrachequesne could you share some info on the best way to gather the internal state of the client when this happens?

The inability to establish a socket.io connection from the typescript client to the python server does happen in about 10% of the cases for me, so I should be able to catch it after a few retries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants