Hi everyone, apologies if this is not the right ch...
# sdk-native
s
Hi everyone, apologies if this is not the right channel for this, but we are building a text to text communication with AI using livekit and trying to integrate with iOS native SDK. However this is what happens : Context We are using the LiveKit Android SDK for real-time chat-style messaging via
publishData()
. Below is the function we’re using:
Copy code
suspend fun sendText(text: String, topic: String = "lk.chat"): Boolean {
    Log.d("LiveKitManager", "canPublishData=" + room?.localParticipant?.permissions?.canPublishData)
    return try {
        val payload = text.toByteArray(Charsets.UTF_8)
        val res = room?.localParticipant?.publishData(
            data = payload,
            reliability = DataPublishReliability.RELIABLE,
            topic = "chat"
        )
        Log.d("LiveKitManager","The res is $res")
        res?.onFailure { Log.e("LiveKitManager", "publishData failed", it) }
        res?.isSuccess == true
    } catch (e: Exception) {
        false
    }
}
Observations 1.
canPublishData
is true for the local participant.
publishData()
returns a success result (no exception thrown). However, the data is not received on the remote participant side. The
topic
parameter passed to the function (
"lk.chat"
) seems to be ignored, and the SDK examples/documentation often show
"lk.chat"
as a default topic. 1. Changing the topic value (e.g.,
"chat"
,
"custom"
) does not appear to affect delivery behavior. Environment • Platform: Android • LiveKit Android SDK (latest stable) • Room is successfully connected • Permissions verified on server side Would mean a lot if someone can guide us along in this.
l
I recommend using sendText: https://docs.livekit.io/transport/data/text-streams/#sending-all-at-once I was going to point you to https://github.com/livekit-examples/android-components-meet/tree/main for an example, but the sendText function in that app is still
todo
(probably because it predates the introduction of sendText in the SDK). Here are code snippets for sending / receiving text in Android: Receiving
Copy code
LaunchedEffect(room) {
                    Timber.d { "Registering text stream handler for topic: $CHAT_TOPIC" }
                    room.registerTextStreamHandler(
                        topic = CHAT_TOPIC,
                        handler = { receiver, identity: Participant.Identity ->
                            Timber.d { "Text stream received from: ${identity.value}" }
                            coroutineScope.launch {
                                try {
                                    val message = receiver.readAll().joinToString(separator = "")
                                    Timber.d { "Message content: $message" }
                                    Toast.makeText(
                                        this@CallActivity,
                                        "${identity.value}: $message",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                } catch (e: Exception) {
                                    Timber.e(e) { "Error reading message" }
                                }
                            }
                        }
                    )
                    Timber.d { "Text stream handler registered successfully" }
                }
Sending:
Copy code
if (showMessageDialog) {
    SendMessageDialog(
        onDismissRequest = { showMessageDialog = false },
        onSendMessage = { message ->
            showMessageDialog = false
            Timber.d { "Sending message: $message to topic: $CHAT_TOPIC" }
            coroutineScope.launch {
                try {
                    val result = room.localParticipant.sendText(
                        message,
                        StreamTextOptions(topic = CHAT_TOPIC)
                    )
                    result.onSuccess { info ->
                        Timber.d { "Message sent successfully, stream id: ${info.id}" }
                    }
                    result.onFailure { error ->
                        Timber.e(error) { "Failed to send message" }
                    }
                } catch (e: Exception) {
                    Timber.e(e) { "Exception while sending message" }
                }
            }
        },
    )
}
This was the result of asking AI to implement the
todo
in the previously mentioned repo, but it seems to have done a good job
s
thanks