socket.io
socket.io copied to clipboard
Socket.IO stops working randomly until page refresh
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!
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! 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.
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?
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: https://github.com/miguelgrinberg/python-socketio/issues/1434
After downgrading to 4.7.5, the issue seems to be gone.
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.
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.
Just to give more info: the downgrade did not help at all , and we are still facing the same issue.
@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.
I'm seeing a similar issue that may be related. When I am connected with a mobile client and put the phone into airplane mode and back out again, about 1 in 10 times the connection does not seem to reconnect, and further messages are ignored.
But what I found when logging was that the server most of the time does disconnect / connect but sometimes does connect / disconnect. In that second scenario, it needs a page reload to recover OR another switch to airplane mode and back off again.
The client side shows that it recovered correctly and is sitting waiting for messages from the server. But the server believes it's disconnected so no longer sends to it.
I connected the admin.socket.io and took the following screenshots, the first is a normal reconnection, the second is an abnormal reconnection.