Skip to content

Using the C++ Embedder API to start and stop Node#3

Draft
jermy-c wants to merge 2 commits intoupdate-libnodefrom
handle-events
Draft

Using the C++ Embedder API to start and stop Node#3
jermy-c wants to merge 2 commits intoupdate-libnodefrom
handle-events

Conversation

@jermy-c
Copy link
Copy Markdown

@jermy-c jermy-c commented Sep 9, 2025

Since we don't Node.js explicitly I thought we could try stopping it whenever the app pauses and that would fix some other issues also.

The libnode uses the API provided by Node.js.

The plan was to:

  1. Handle pause, resume, stop, restart events accordingly and manage how node.js runs
  2. Implement a properly working node::Stop function
  3. Call node::Stop
  4. Start node faster

However, I realized that.

  1. This won't fix our problem with backend loading after the frontend loads because the backend loads after the Plugin is registered and the Plugin is registered only after Capacitor is running and properly setup
  2. I don't really understand how the API works and there's not a lot of docs about it
  3. We are running C++ in the JNI layer so the static variables for the ENV might not work as expected
  4. Or how do we store the ENV in Java or JS to for the node::Stop() to use

https://nodejs.org/api/embedding.html#c-embedder-api

@benbucksch
Copy link
Copy Markdown

Can we load node.js not as capacitor plugin, but rather link it directly from the Android project as native dynamic library that is required by our app binary? Then the OS would link it for us at startup.

You'd still need to call node.js own start()/stop() functions, but at least node.js would be guaranteed to be loaded.

Our first attempts on iOS did that as well.

@jermy-c
Copy link
Copy Markdown
Author

jermy-c commented Sep 9, 2025

Yes, that makes sense. I see iOS Node.js mobile doesn't use the same Plugin pattern. I'll try that.

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

Successfully merging this pull request may close these issues.

Mobile: Manage the backend according to native app lifecyle

2 participants