I am working on a chatting app using Kotlin, XML, Android Studio and Supabase(for the back-end).
I am facing a problem with the real time feature so that I can handle real time messaging between users.
private suspend fun realtimeCheck(scope: CoroutineScope){
try{
println("here")
channel = supabase.channel(chatRoomId)
val dataFlow = channel.postgresChangeFlow<PostgresAction.Insert>(schema = "public"){
table = "message"
}
println("here 2")
dataFlow.onEach { change ->
println("Reach in here $change")
val stringifiedMessage = change.record.toString()
val data = Json.decodeFromString<data.message>(stringifiedMessage)
messageContainer.addMessage(data)
runOnUiThread {
recyclerViewMessages.invalidate()
}
}.catch{e -> e.printStackTrace()}.collect()
println("Data Flow: $dataFlow")
println("after loop")
channel.subscribe(blockUntilSubscribed = true)
println(channel.status.value)
}catch(e: Exception){
e.printStackTrace()
Toast.makeText(this@ChatScreen, e.message, Toast.LENGTH_SHORT).show()
}
}
This function deals with the real time aspect of supabase by creating a channel, and subscribing to it, the onEach{}
code block is supposed to update the list of messages by adding the new message and then call .notifyItemInserted(messageList.size - 1)
to update the RecyclerView so the user can see the new message in real time(be it a received message or sent).
My problem is that the dataFlow.onEach{}.collect()
never actually executes even when an insert operation happens on Supabase(on the Supabase website or via the API in Kotlin) and blocks the functions after from running, so println("Data Flow: $dataFlow")
and after never runs.
I tried using .launchIn(scope)
but that only allowed execution to continue and reach the rest of the function.
That did allow me to see that the println(channel.status.value)
line prints SUBSCRIBED
. However I still did not get an output from the code block inside the .onEach{}
block.