GoodDAPP icon indicating copy to clipboard operation
GoodDAPP copied to clipboard

Research: service workers / background fetch

Open johnsmith-gooddollar opened this issue 2 years ago • 8 comments

Currently we have service worker auto generated by the workbox. It manages web app caching and updating

  • [ ] could we register more than one SW ?
  • [ ] if not how we could integrate our code to the SW generated by the workbox ?
  • [ ] our SW should be built by webpack, it should use some app code and libraries
  • [ ] maybe some app code should be adjusted. for example, goodwallet was refurbished to be used by hooks. Maybe we would need to add option to initialize it the old way (to be used in worker)
  • [ ] ideally we may need to have some minimalistic version of the wallet class for service worker (which performs only the operations needed - subscription, checking entitlement etc)
  • [ ] or maybe we could just pass ref to the goodwallet down to the SW ?
  • [ ] the code should be included to the both SW and background fetch (on RN)

johnsmith-gooddollar avatar Jun 23 '22 09:06 johnsmith-gooddollar

  • for use serviceWorkers with RN we can use library like 'react-native-workers' (https://www.npmjs.com/package/react-native-workers)
  • we couldn't create more then one serviceWorker because it would rewrite previous. There is one workaround solution to create multiple serviceWorkers: https://github.com/facebook/create-react-app/issues/1277#issuecomment-313950613
  • the only way to communicate with SW is by messaging, so we can emit messages on wallet events which we listen to and add the appropriate listener inside the worker itself, thus using it as something like a reducer with action types. To implement this, we can use Broadcast Channel API (https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) or MessageChannel API. But not sure that solution will works correct with foreground state.
  • possible solution for execute it in background can be next libraries: react-native-background-job react-native-background-task and Headless JS module, but it needs more research

alt-a1r avatar Jun 24 '22 08:06 alt-a1r

  • for use serviceWorkers with RN we can use library like 'react-native-workers' (https://www.npmjs.com/package/react-native-workers)

we already using RNBackgroundFetch. please check how it implemented does it working and will it be suitable for this task

please look how we could add custom code to the SW generated by the workbox

  • the only way to communicate with SW is by messaging, so we can emit messages on wallet events which we listen to and add the appropriate listener inside the worker itself, thus using it as something like a reducer with action types. To implement this, we can use Broadcast Channel API (https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) or MessageChannel API. But not sure that solution will works correct with foreground state.

i've already done with this: #3766

  • possible solution for execute it in background can be next libraries: react-native-background-job react-native-background-task

should be service worker

johnsmith-gooddollar avatar Jun 24 '22 13:06 johnsmith-gooddollar

@johnsmith-gooddollar webworkers are irrelevant we need service worker in any case I suggest to first get Android/iOS working as we already have the boilerplate code for that then start with browser notifications

sirpy avatar Jun 26 '22 05:06 sirpy

@alt-a1r @johnsmith-gooddollar @patpedrosa

  1. for web notifications it is not possible to wakeup the service worker unless using "push notifications" or the new experimental "periodicsync" API
  2. on Safari browsers notifications are pretty new and only supported in the latest safari and macos 13

see: https://stackoverflow.com/a/29750936/6379521

and see https://whatwebcando.today/scheduler.html

My suggestion as mentioned elsewhere is to hold on with web notifications. And in the first phase for web support only claim reminder only for Chrome with the periodicsync api

sirpy avatar Jun 26 '22 08:06 sirpy

@sirpy should we proceed with Chrome + periodicsync to implement claim notifications on web ?

johnsmith-gooddollar avatar Aug 16 '22 09:08 johnsmith-gooddollar

discuss with Pat. perhaps as part of goodswap.xyz

sirpy avatar Aug 16 '22 11:08 sirpy

no good solution for web at the moment.

sirpy avatar Apr 10 '23 08:04 sirpy

@sirpy I've done MVP using Firebase messaging for deliver pushes some time ago. Have you looked onto it ?

https://github.com/GoodDollar/ScheduledNotifications

johnsmith-gooddollar avatar Apr 10 '23 12:04 johnsmith-gooddollar