threema-ch / threema-web

The Threema Web application.
GNU Affero General Public License v3.0
1k stars 107 forks source link

[Question] iOS app background #930

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

Out of topic question, how is Threema iOS App keeping open/alive connections to threema-web while in background.

I am developing a Threema-web like web service (not a messaging app), where we use SaltyRTC and some ideas from your amazing app. Any information on this topic will really help me.

Thanks.

dbrgn commented 4 years ago

It doesn't. There is no way to keep an open connection on iOS without having the phone unlocked and the app permanently open.

The app simply reconnects when it receives a push message and then syncs the state with Threema Web.

Maybe the pages in the "Concepts" section at https://threema-ch.github.io/app-remote-protocol/ help. Keeping in sync is actually not easy, because any message chunk can get lost at any time. Therefore we implemented a custom acknowledgement protocol.

ghost commented 4 years ago

Big thanks for sharing this.

Why didn't you use VOIP ?

dbrgn commented 4 years ago

I don't understand your question.

ghost commented 4 years ago

Sorry, my bad, looks like that API was deprecated.

dbrgn commented 4 years ago

In case you mean VoIP-Pushes: Besides the deprecation for non-VoIP use cases, those only help waking up your device, but the process will still be killed by iOS after ~30 seconds.

Keeping a persistent connection on iOS will not work, unless you keep the app open and the screen unlocked.

ghost commented 4 years ago

Thanks. I will try next days to experiment with reconnect, and see how it will work.

What I am doing now: maybe somehow can help Threema too.

  1. User start the App
  2. Scan QR
  3. Get piped (Responder is connected to Initiator)
  4. App is going to background
  5. beginBackgroundTask(), observing UIApplication.shared.backgroundTimeRemaining
  6. If we have less then 10 sec ...
  7. Send a push to myself, (through relay)
  8. goto 5. Above is keeping app alive, every received push is extending app life for 30 more seconds on iOS 13.

However... if Apple will throttle pushes, or user will have a bad internet connection, app will be suspended.

lgrahl commented 4 years ago

I'm excited about seeing SaltyRTC being used anywhere but from our own experience: You are entering a world of pain and it's unlikely to work reliably now or any time in the future (unless Apple's policy changes). They don't want permanent background activity and they are relentlessly working on enforcing that (see the iOS 13 Push Kit changes).

ghost commented 4 years ago

We have write SaltyRTC in swift, with little modifications to Tasks (can be fixed to follow the Protocol). But still, can fully be used with your Server implementation. We have plans to open source it, but after we launch our product, I hope it will be soon :)

Big thanks for sharing your work.

dbrgn commented 4 years ago

If you're done with the Swift implementation, we'll be happy to link to it from https://saltyrtc.org/pages/implementations.html :slightly_smiling_face:

ghost commented 4 years ago

@dbrgn Hi, we finally have open sourced it https://github.com/passlock/SaltyRTC There are some things to finish, but can be used already, as we are doing in @passlock

dbrgn commented 4 years ago

@projectvim congrats! Feel free to open a PR to add it to https://github.com/saltyrtc/saltyrtc-website/blob/master/content/pages/implementations.md and maybe to https://github.com/saltyrtc/saltyrtc-website/blob/master/content/pages/users.md as well :slightly_smiling_face: