Scarlet icon indicating copy to clipboard operation
Scarlet copied to clipboard

Duplicate response from websocket is coming after a channel is unsubscribed and subscribed again

Open rohitiiita007 opened this issue 3 years ago • 0 comments

I have a use case where I unsubscribe a channel and then subscribe again. But somehow multiple duplicate response is received . Below are my subscribe & unsubscribe methods `

fun subscribeChannel() {
     viewModelScope.launchWithException {
            if (socketRepository.isSocketConnected) {
                socketRepository.subscribeChannel(coinName, ChannelNameConstants.CHANNEL_ORDER_BOOK)
            } else {
                val socketFlow = socketRepository.getWebSocketEvents()
                socketFlow.collect {
                    when (it) {
                        is WebSocket.Event.OnConnectionOpened<*> -> {
                            socketRepository.isSocketConnected = true
                            socketRepository.subscribeChannel(coinName, ChannelNameConstants.CHANNEL_ORDER_BOOK)
                        }
                        is WebSocket.Event.OnConnectionClosed,
                        is WebSocket.Event.OnConnectionFailed -> {
                            socketRepository.isSocketConnected = false
                        }
                    }
                }
            }
        }
}
    fun unsubscribeChannel(coinName: String, channelName: String) {
    socketService.sendUnsubscribe(
        ChannelData(
            channelName = "$channelName ${SubscriptionType.UNSUBSCRIBE.value}",
            data = coinName
        )
    )
}
`

rohitiiita007 avatar Apr 26 '22 10:04 rohitiiita007