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

Websockets loop greatly slows down if a client WS lost due to Wifi dropout or change of SSID #922

Open
computski opened this issue Dec 16, 2024 · 5 comments

Comments

@computski
Copy link

I have a websocket server running on an ESP8266. SSID = "something" Clients connect to something-WiFi, invoke a webpage and a websocket is established behind this. If the client closes the web page whilst still conneted to SSID-something, all is well. Multiple clients can connect/ disconnect like this.

BUT, if a client is on SSID-something with an open webpage (and so an open websocket) and then either the WiFi drops out or the client navigates to a different SSID-someOther then, after a few seconds, the whole websocket loop on the ESP greatly slows down permanently. The only recovery is a reboot.

Is there an error event I should handle? is there a way to detect 'lost clients' like this and terminate them on the ESP?

@xavier-offc
Copy link

I also encountered this problem. Didn't find a solution?

@computski
Copy link
Author

The issue is still open, the developer has not commented. I have not spent any time on a solution myself, possibly one could use the heartbeat mechanisms to detect a lost WS client and then execute a close on the client link.

@xavier-offc
Copy link

That's what I did yesterday. I used enableHeartbeat(2000, 1000, 3) and the problem seemed to be solved.

@computski
Copy link
Author

Hey good work. Can you post a code snippet showing the event handler you used along with enableHeartbeat. Thanks!

@xavier-offc
Copy link

xavier-offc commented Jan 29, 2025

I am writing a project for interaction via WI-FI between a device (ESP32) and a tablet (Android/Kotlin).

ESP32 is responsible for the WebSocket server in STA WI-FI mode (communication occurs via a Mikrotik router)

My problem was that the user with the tablet could move away to a long distance, due to which the connection to WI-Fi could be lost - this is how I came to the problem you described.

Without using heartbeat, I noticed that disconnection detection could occur only after 1-3 minutes.

Using heartbeat - disconnection detection occurs almost at the moment when the loss of connection to WI-FI began.

It also turned out that the problem was more in the broadcastTXT function (because there were definitely attempts to send a message even to actually disconnected clients).

Below is a piece of code that I use in my project to work with WebSocket.

I hope this information will help you move forward in solving your specific problem.

WebSocketsServer socket = WebSocketsServer(5555);
socket.begin();
socket.enableHeartbeat(2000, 1000, 3);
socket.onEvent([&, this](uint8_t num, WStype_t type, uint8_t* payload, size_t length) {
  if (type == WStype_DISCONNECTED) {
    Serial.println("Client ID" + String(num) + " has been disconnected");
  }
  if (type == WStype_CONNECTED) {
    if (this->syncReceiver) {
      this->syncReceiver(num);
    }
  } else if (type == WStype_TEXT) {
    String msg = this->parseMessage(payload, length);
    msg.trim();
    this->processMessage(msg, num);
  }
});

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

No branches or pull requests

2 participants