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" />