Offline Apps - Wake when "Back-online"

This might be a feature request!! Or hopefully there’s a work around.

I’m developing an app that will have offline functionality part of which will be messaging.

I want to allow users to send messages when offline and have the app send them when the connection returns.

The only way I can think of achieving this is with access to the Service Worker.

Either using background sync to send the message itself or perhaps even using a dummy background fetch with a callback to the app on success.

The Service workers are there we just don’t have access to them - or do we?.

If any of the anvil developers have a spare five minutes to knock up a “Back-online” callback function that would be awesome :smiling_imp:

You could store messages in the browser’s local storage and use a server call to send any outstanding messages from there periodically.

You might want to have a look at the storage module of anvil extras for using the browser’s local storage.

You might also be interested in the non-blocking module of anvil-labs for doing the periodic server call.

If you really want to use a service worker (I don’t think you need to), you could look at web-workers in anvil labs.

Thanks for the links but the issue as I see it is the client side app may be dormant when “back-online” therefore no way to trigger the send.

The scenario is:

  • pick up phone
  • open app
  • type message and click (even if offline)
  • close app/put phone back in pocket

I can save the messages to local storage.

I can even re-send them next time the app is opened and “back-online”.

What I can’t do is send them when “back-online” while the phone is in their pocket or they’re doing something else and the app is closed.

I need some way to wake the app when “back-online”.

Hi Owen,

Sorry my first negative response was a bit too quick off the mark.

I’m way out of my experience level here so my understanding of the issue maybe wrong.

Could you perhaps go into a little more detail regarding how the non-blocking module would handle the app going dormant (mobile android, switched to another app, or phone off (sleep)).

Also will the web-workers module wake the app if it has gone dormant (as above).

The work done in creating web-workers does at least prove that it should be possible to create a function like I described in my OP of a wake on “back-online”. Although it would be beyond me I fear.

Is it something that @owen.campbell /@stucork would take on?

Actually, having thought a little more, I think you might need the service worker after all to handle background calls when the app is closed.

I think my original idea of waking the App when back online then doing the update (Message send to server) from the app was wrong.

What I need is the update to happen as soon as the device is back online even if the app is not open.

What would be better is some kind of async client to server update that is independent of the app being open. A Service worker using background sync perhaps direct from the local storage with some mechanism to keep track of status of the update.

Its my understanding that service worker background sync hands the work to the browser and then even the OS to perform the task independent of the calling entity being active/asleep.

Does this sound possible?

I’d say that sounds possible, yes. Leading edge potentially, so all the usual caveats, but yes, within the realms of possibility.