nodejs-mobile / nodejs-mobile

Full-fledged Node.js on Android and iOS
https://nodejs-mobile.github.io
Other
452 stars 44 forks source link

Background behaviour on iOS #109

Open libf-de opened 3 weeks ago

libf-de commented 3 weeks ago

Version

v18.17.2

Platform

iPadOS 17.0.1

Subsystem

No response

What steps will reproduce the bug?

Use the provided sample app. If the app is not being debugged with Xcode and the iDevice gets turned off, the webserver is no longer reachable, even after turning the device back on. Then attaching the Xcode debugger shows that the NodeJS is still running, but the server is no longer reachable. With no possibilities of killing the thread, it's impossible to restart the server without restarting the whole app.

How often does it reproduce? Is there a required condition?

Always, if the app wasn't started from Xcode Debugging.

What is the expected behavior? Why is that the expected behavior?

I understand iOS makes it hard to let apps run in the background, and I don't intend to do that. But sane behaviour when the app get's put in the background is expected (I think), e.g. by terminating the NodeJS thread to be able to restart it.

What do you see instead?

An unreachable web server with the NodeJS thread still running

Additional information

Trying to (re)start the server anyway will result in the whole app crashing. The device used for testing is an iPad Air 5 running iPadOS 17.0.1.

thinhkaku commented 2 weeks ago

Same problem? Does anyone have a solution?

pablogeek commented 3 days ago

same here, any solution?

Chasky-H commented 1 day ago

We encountered this issue using express.

iOS terminates open http ports when app is moved to background after a while.

We solved this by calling the close() function on the http.Server instance returned by the express app.listen function on the UIApplication applicationDidEnterBackground, and restarting the express server when app is resumed in applicationWillEnterForeground. You should probably be fine doing both in applicationWillEnterForeground as well.

libf-de commented 1 day ago

We encountered this issue using express.

iOS terminates open http ports when app is moved to background after a while.

We solved this by calling the close() function on the http.Server instance returned by the express app.listen function on the UIApplication applicationDidEnterBackground, and restarting the express server when app is resumed in applicationWillEnterForeground. You should probably be fine doing both in applicationWillEnterForeground as well.

How do you implement this interaction between Swift and NodeJS? All I could find was calling API endpoints, which rules out starting express this way. I currently terminate the whole app when going into background as this is not an expected scenario, but the app should behave predictably if this happens on accident.

Chasky-H commented 1 day ago

We implemented a message-based bridge between the native application and the node.js app.

It required implementing a native addon (we used napi), and leaving behind an asynrounous funciton that can be called from threads other than the nodejs main thread, see. You can also check out how this was done in the cordova and react native plugins.

This is quite a hassle and there is a steep learning curve if you haven't used napi before.