0

in Jetpack Compose (1.0.4) I am using Tinder/Scarlet(2.4.0) for connecting WebSocket

implementation 'com.github.Tinder.Scarlet:scarlet:0.2.4'
implementation 'com.github.Tinder.Scarlet:scarlet-lifecycle-android:0.2.4'
implementation 'com.github.Tinder.Scarlet:scarlet-message-adapter-moshi:0.2.4'
implementation 'com.github.tinder.scarlet:scarlet-protocol-websocket-okhttp:0.2.4'

and thats how i init and describe it on hilt

@Provides
    fun providesApplication(@ApplicationContext context: Context): MyApplication{
        return context as MyApplication
    }



@Provides
    fun provideScarlet(
        application: MyApplication,
        client: OkHttpClient,
        moshi: Moshi
    ): Scarlet {
        val protocol = OkHttpWebSocket(
            client,
            OkHttpWebSocket.SimpleRequestFactory(
                { Request.Builder().url(baseWebSocketAddress).build() },
                { ShutdownReason.GRACEFUL }
            )
        )

        val scarletConfiguration = Scarlet.Configuration(
            messageAdapterFactories = listOf(MoshiMessageAdapter.Factory(moshi)),
            streamAdapterFactories = listOf(FlowStreamAdapter.Factory()),
            backoffStrategy = LinearBackoffStrategy(500),
            lifecycle = AndroidLifecycle.ofApplicationForeground(
                application
            )
        )
        return Scarlet(protocol, scarletConfiguration)
    }

@Singleton
@Provides
fun provideScarletMessagingService(scarlet: Scarlet): ScarletMessagingService {
    return scarlet.create()
}

everything works well including events , observe messages and the others the problem here when I close/minimize the application websocket get closed even with implementing:

   backoffStrategy = LinearBackoffStrategy(500),
   lifecycle = AndroidLifecycle.ofApplicationForeground(
        application
    )

and i am using scarlet instance on my viewmodel like this

@HiltViewModel
class MessagesViewModel
@Inject constructor(
    private val scarletMessagingService: ScarletMessagingService) : ViewModel() {
       init {
            scarletMessagingService.observeMessage()
        .flowOn(Dispatchers.IO)
        .onEach {
            Timber.d("A message from server $it.toString()")
        }
        .launchIn(viewModelScope)
       }
}

and also i have this permission

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121
ArtixModernal
  • 661
  • 2
  • 8
  • 21
  • 2
    How is that connected to Jetpack Compose? You didn't provide the most important code i guess. – Jakoss Nov 04 '21 at 12:13
  • @Jakoss i updated rest of the viewmodel and it works well , i dont think so the rest of the code is important, i just wanted to say i built my project in jetpack compose still did not try to show the message in view – ArtixModernal Nov 04 '21 at 12:23
  • 1
    I guess information about compose is just unnecessary noise in your question, i'd think about removing that – Jakoss Nov 04 '21 at 13:14

1 Answers1

0

Foreground service permission is not magical switch that your app can now work in background. You have to actually implement service and run it as a foreground one. In that service you have to implement the actual handling of the websockets.

You can find out how to implement that on official docs: https://developer.android.com/guide/components/foreground-services

Jakoss
  • 4,647
  • 2
  • 26
  • 40
  • i guess its not the best solution , because Tinder already developed it , take a look at https://medium.com/tinder-engineering/taming-websocket-with-scarlet-f01125427677#:~:text=To%20keep%20the%20connection%20open%20only%20when%20the%20app%20is%20in%20the%20foreground%2C%20you%20can%20write%20a%20Lifecycle%20for%20the%20app%20foreground%20scope.%20(Scarlet%20comes%20with%20an%20AndroidLifecycleplugin%20so%20that%20you%20don%E2%80%99t%20have%20to%20write%20one – ArtixModernal Nov 04 '21 at 14:02
  • It's the only solution, the only thing Tinder did is integrate with android lifecycle. You should bind your connection to the service lifecycle (which is android lifecycle too) you have to create – Jakoss Nov 04 '21 at 14:32