microsoft / react-native-windows

A framework for building native Windows apps with React.
https://microsoft.github.io/react-native-windows/
Other
16.35k stars 1.14k forks source link

Headless JS (backgroundtask) support #5375

Open achipa-HP opened 4 years ago

achipa-HP commented 4 years ago

Proposal: Headless JS (backgroundtask) support, take 2

Summary

Provide functionality and examples that allow for headless JS support in backgroundtasks

Motivation

Many of the system and lifecycle events in the UWP model land in backgroundtasks. Not having a (documented way) of having that functionality runs the danger of code duplication and/or hacks that use React Native for Windows in the wrong way to achieve it.

Open Questions

This has already been requested in https://github.com/microsoft/react-native-windows/issues/1993 but was closed, as at that time, current was already on it's way out. #3365 was pointed to as the solution for vNext, but that PR was closed in favor of a ReactNativeHost solution. As of the time of this request, it's unclear if that (ReactNativeHost) solution is now viable, available or recommended, and there is no documentation/examples to confirm it.

chrisglein commented 4 years ago

Can you give some background on what your scenario is for this? We want to understand how you would like to integrate UWP's notion of background tasks with React instancing. What are you trying to accomplish?

achipa-HP commented 4 years ago

@chrisglein The classic scenarios of headlessJS - like for data prefetching or precalculation when the conditions are right and app is not in the foreground, but also notifications and custom triggers. All this for in-process backgroundtasks.

Think https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.background.systemtriggertype?view=winrt-19041

Example 1: Online status changes:

Example 2: Push notifications

Example 3: Background work cost changes (trying to be mindful of battery resources):

Example 4 (yes, this is for an OEM) : Receiving custom trigger events (as per https://docs.microsoft.com/en-us/windows-hardware/drivers/partnerapps/background-tasks-and-oem-triggers)

All these today go through BackgroundTasks in some shape or form, and there is no (good? recommended?) way of relaying these events into RNW unless the app is in the foreground.

chrisglein commented 4 years ago

@stecrain I believe you have some experience in this. Any chance you can give a quick summary? And then at some point we should probably write something on our docs website similar to the coverage on this topic for iOS and Android. It does seem like we should address it there.

achipa-HP commented 4 years ago

FWIW Upstream React Native already has an implementation for Android (if it's easier to align and not reinvent new APIs, as the concept is the same, regardless if it's a UWP BackgroundTask or Android)

https://reactnative.dev/docs/headless-js-android

harinikmsft commented 4 years ago

Marking this as needs triage to bring it up for discussion. Based on conversations offline, here are the findings:

achipa-HP commented 4 years ago

Marking this as needs triage to bring it up for discussion. Based on conversations offline, here are the findings:

  • That native module only supports time based triggers. For scenarios that require more complex/other triggers, more investigation may be needed.

The documentation suggests it goes beyond time triggers (unless you're referring to Windows-specific implementation details). The very example at https://reactnative.dev/docs/headless-js-android#example-usage provided for Android demonstrates HeadlessJS use on a trigger (intent) invoked by a network status change - not a timer.

There is another approach via https://www.npmjs.com/package/react-native-background-timer which is timer-specific, but that's largely unrelated to the HeadlessJS use-cases of upstream React Native (and this particular GH issue).

mjfusa commented 3 years ago

Wrote a POC for registering and triggering JavaScript background task (startHeadlessTask). Windows triggers invoke OnBackgroundActivated in App.cpp. Here a Notification is sent to native module to run background JS.

Background task with / without app running scenario status: Foreground - Working Background - Working Background Suspended - Working Not Running - Not Working. It seems this is blocked due to the initialization of several components on instance creation that require the UI thread,

More in the readme

Code here: https://github.com/mjfusa/react-native-windows See the branch backgroundtask.

rohitgaidhane commented 1 year ago

When this will available this is very necessary to create messaging or realtime App

AhmedMustafa505 commented 10 months ago

I'm implementing a chat application using RNW and this service is crucial for receiving messages while on background state and sending message status responses back to the server.