Closed bahientn9x closed 5 years ago
It sounds like you're talking about two different problems:
1) Automatically reconnecting when a connection is lost (assuming the app is not suspended). 2) Preventing the app being suspended, so that you can keep a connection open and process messages while in the background.
The first problem is definitely something this library should do - I'm happy to take PRs on that one but I'll hopefully get around to implementing it at some point.
The second problem is independent of this app, and is down to the OS (and Android and iOS have some different approaches here). For the fundamentals you'll probably want to read about iOS background execution and Android background services. The method you use will depend on whether you really need a continuous connection, whether it's good enough to wake the app up on a schedule, or whether you want to wake the app up at a time decided by the server.
I need to stay connected in my app even when the screen is off. Because every time I turn off the screen, the connection is immediately shut down. Is there a way to handle this problem? I do not have much experience in handling this problem. Can you give me some idea?
Do you really need to stay connected, or do you just need to automatically reconnect when the app comes back to the foreground? The latter is fairly easy, the former is not (the OS wants to suspend your app whenever possible in order to conserve battery and resources).
I'm guessing you don't really need to stay connected, because that would mean that your app breaks whenever a force closes your app, loses their network or turns off their phone, and there's no way to prevent any of those things happening. I'd suggest implementing automatic reconnect (you can use AppState
to detect state changes in react native) and thinking about how to gracefully resume from a lost connection.
Currently I'm using appState to reconnect and it works perfectly, but it's annoying if the user accidentally presses off the screen and has to actually reconnect causing a waiting period. So I really need a continuous connection or at least keep it running after a period of time in the background
Ok, but keeping your app alive in the background is really between your app and the OS - it doesn't have anything to do with this library.
If you only want to extend your app's running time by a few more seconds (on iOS) you might try extending the background time.
I wouldn't recommend any of this, because it seems like you're fighting the operating system (and second-guessing the user's intentions) for the sake of saving a small (<1s?) reconnection time, and in my experience it takes a lot of work to get right. In any case I'm afraid it's out of scope here. Good luck though.
I have a problem with maintaining the mqtt connection. When I turn off the screen on the ios or the application works on the background for about 30 seconds, the connection is immediately closed. I want to stay connected even when working in the background. Is there a way to do this? It is very important to me that if I do not maintain the connection, when turning off the screen I have to re-make the connection, it causes discomfort to the user. Thank you very much for the library. Looking forward to your response