JaneaSystems / nodejs-mobile

Full-fledged Node.js on Android and iOS
https://code.janeasystems.com/nodejs-mobile
Other
2.58k stars 182 forks source link

Run Node JS thread even when the app is closed #311

Open vasusharma7 opened 3 years ago

vasusharma7 commented 3 years ago

Project: nodejs-mobile-react-native

Mobile OS and version: Android 10

Development Node.js: v14.15.1 Development platform: Ubuntu 20.04

I want to run an HTTP server ( and do some other jobs ) even when the app is closed. When the app goes to background, things work fine but when the app is closed, the server stops. Is there a way to run the Node JS thread continuously in background even after app is closed ?

vasusharma7 commented 3 years ago

I was able to achieve this by tweaking the JAVA code of the module to run a background android service for node js. Now the node http server runs even when the app is closed. Did only for Android.

Please let me know if anyone wants to see the code !

vasusharma7 commented 3 years ago

@joaocgreis @orangemocha @enricogior

Can we provide an optional argument in nodejs.start("main.js") to either run NodeJS as a background thread (which is terminated when the app is closed as it is with current code) or as a background service in Android (which persists even on closing the app).

I could see there are many issues open related to keep running NodeJs in the background even on closing the app. ( Particularly useful in cases where users want to run HTTP server in NodeJS continuously in background. )

I will be happy to contribute towards this. I have already done that for android, have been testing it for a while and working good. Please let me know, I will open a PR.

jaimecbernardo commented 3 years ago

Hi @vasusharma7 , This sounds like a valuable contribution, even as documentation for other users trying to achieve the same. Feel free to open a PR. Thank you!

vasusharma7 commented 3 years ago

Thank you for the response. I will refactor my code and open a PR soon.

Thanks !

Tkd-Alex commented 3 years ago

Hi! I'm super interested on this, Can I look at the code? Maybe a preview? :rofl:

introspection3 commented 3 years ago

@vasusharma7 hi ,sir,could we use you code now?

voxsoftware commented 2 years ago

I was able to achieve this by tweaking the JAVA code of the module to run a background android service for node js. Now the node http server runs even when the app is closed. Did only for Android.

Please let me know if anyone wants to see the code !

@vasusharma7 Please give us the code, and what about the PR?

vasusharma7 commented 2 years ago

You can find all the changed files here - https://drive.google.com/drive/folders/17bXb9t0PIEJYU8XCWEDNovjyhyXgsEkC?usp=sharing Look out for 'src' folder .

How to achieve this - After installing the package from npm -

What are the changes

static {
System.loadLibrary("nodejs-mobile-react-native-native-lib");
System.loadLibrary("node");
}

There is one more change in CPP file. Inside src/main/cpp/native-lib.cpp file, I have added another function

Java_com_janeasystems_rn_1nodejs_1mobile_NodeService_startNodeWithArguments()

The CPP File requires this function name to be in format of path of the Java file from where the function "startNodeWithArguements" is called. Here in this case, it will be

Java/com/janeasystems/rn_1nodejs_1mobile/NodeService

so the name in CPP file translates to the mentioned above.

The function body is copied from the function just above it.

Didn't get time to refactor the code and raise a PR with proper documentation etc., will do it as soon as I get time

I was able to achieve this by tweaking the JAVA code of the module to run a background android service for node js. Now the node http server runs even when the app is closed. Did only for Android. Please let me know if anyone wants to see the code !

@vasusharma7 Please give us the code, and what about the PR?