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

When the computer enters sleep mode, the MQTT client no longer sends heartbeats. #1761

Open
LiuZiqi-dual opened this issue Dec 8, 2023 · 3 comments

Comments

@LiuZiqi-dual
Copy link

To solve #1361 ,I've tried MQTT version 5.3.3 recently, it's able to maintain the connection automatically when client browser tab are blured or the whole browser is minimized, which works well.

However, when the computer enters sleep mode, the heartbeat stops sending immediatly.And the weirdest thing is, I purposefully added a close callback in the MQTT method to reconnect after disconnection. However, after the computer goes into sleep and then wakes up, none of the console.log in the close callback are printed. It seems like the JavaScript code in the callback hasn't been executed at all. By the way, In my tests, I tried to manually disconnect the client from the server while in the awake state. This does trigger the code in the close callback, and it can reconnect normally.

this.client.on("close", async err => {
  // console.log("MQTT 连接关闭");
  console.error("MQTT 连接关闭", err, new Date().valueOf());

  // 如果已经尝试重连超过10次,就不再重连
  if (this.retryCount >= 10) {
    console.error("已经尝试重连10次,不再重连");
    return;
  }
  // 每秒发起一次重连,重连次数超过10次后,不再重连
  setTimeout(() => {
    this.retryCount++;
    this.connect();
  }, 1000);
});

I'm not sure if this is a problem with this MQTT.js library, the browser, or the Windows operating system.

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (16) x64 AMD Ryzen 7 7840H w/ Radeon 780M Graphics
    Memory: 13.54 GB / 29.71 GB
  Binaries:
    Node: 16.17.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.15.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.10.3 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Spartan (44.22621.1702.0), Chromium (100.0.1185.36)
    Internet Explorer: 11.0.22621.1

@robertsLando
Copy link
Member

If that happens means the pc also pauses the worker thread, I think that's an os issue or maybe a setting to save power not an issue with MQTTjs

@LiuZiqi-dual
Copy link
Author

If that happens means the pc also pauses the worker thread, I think that's an os issue or maybe a setting to save power not an issue with MQTTjs

I also assumed the same, but a few weeks ago, I was still using the v4.x version of MQTT.js. I specifically wrote a mqtt.worker.js in my local project to handle this issue, and it worked very well, able to maintain the connection all night. When I put my computer to sleep at night, then open it in the morning, I can see the print log in the browser console. The client continues to loop through the "disconnect-reconnect" sequence as expected.

However, in recent days I've noticed that it seems like after entering sleep mode, all of the JavaScript is unable to execute in the background. Instead, it seems to pile up and only executes at the moment the machine is woken up. I am trying to figure out why this is happening.

@robertsLando
Copy link
Member

robertsLando commented Dec 8, 2023

Try to see if this is a known issue with worker-timers npm module or something OS specific

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