1

For my chat app I am looking for a good way to sync messages while the app is in background.

At the moment I have a websocket. When the app comes to foreground, messages get synced. This makes the app looking laggy. Like WhatsApp I need a way to have all messaged synchronized before the app gets in foreground.

I thought about the following solution: I set up a SyncAdapter which gets triggerd by a push notification ("new message for chat xy"). Then inside the SyncAdapter I fetch new messages.

Now the problem is I do not want to connect (e.g. via REST) for each line which has been written. And also I don't want to wait x minutes to bundle new messages.

As I said before, I already have a websocket which gets established when the app goes to foreground. Is it a good idea to move this code to the SyncAadapter? Can I keep the connection open there or should I close it if I dont receive a message for x minutes? The only problem I see is, if an user is not able to receive push messages.

If anyone knows of famous chat apps do it, please point me to this direction.

NoobieNoob
  • 887
  • 2
  • 11
  • 31

1 Answers1

1

Signal (secure messaging app) is opensource. You can take a look at how they implemented a real-world solution with the goal of implementing a very secure chat.

Depending of what is your main goal you can choose a simpler implementation with available pieces. However I think that you still need a few pieces:

  1. A push system to wake up your application when a new message is available
  2. A way to retrieve the message when the app is in background
  3. A system that monitor network and retries sending a message as soon network is back

The first item can be solved with Firebase Cloud Messaging (FCM) the second and third can be implemented with WorkManager.

I would avoid to keep something on the device with an open connection to check for new messages for two main reasons:

  • battery consumption
  • background restrictions
pfmaggi
  • 6,116
  • 22
  • 43
  • Thanks, but imagine multiple users are chatting, new lines every few seconds. How to implement 2.? A rest call for each message would take too much battery I think. – NoobieNoob Oct 29 '20 at 08:18
  • My point 2 covers when the app is in the background. I'm also not sure that having the connection always active vs doing a REST call for each message is much different from a battery point of view. I would do some tests and use the battery historian to check. But you can also have a different way to keep the connection/communication active while the user has the app in foreground. – pfmaggi Oct 29 '20 at 08:29
  • You mean it is possible to create a shared connection which can be used by: the frontend thread and the background thread (workmanager)? – NoobieNoob Oct 29 '20 at 08:32
  • You can pass the connection to the Worker using a custom WorkerFactory: https://medium.com/androiddevelopers/customizing-workmanager-with-dagger-1029688c0978 Also Hilt has some support for this: https://developer.android.com/training/dependency-injection/hilt-jetpack – pfmaggi Oct 29 '20 at 14:56