Android and Kotlin

A native Kotlin client for Android (minSdk 24) and any JVM. It speaks the same realtime protocol as the JavaScript SDK and behaves the same way: it reconnects with backoff, catches up on missed messages by seq after every connect, and keeps unsent messages in an outbox. It uses coroutines throughout: StateFlow for connection state and message lists, SharedFlow for events.

Chat only: for voice and video calls, use the web or React Native SDK.

1. Install

The SDK is not on Maven Central yet. Build it from the android/ folder of the SDK repository and publish it to your local Maven repository:

cd byotalk-sdk/android
./gradlew :byotalk:publishToMavenLocal
// settings.gradle.kts: repositories { mavenLocal(); mavenCentral() }
dependencies {
    implementation("com.byotalk:byotalk:0.1.0")
}

byotalk is the core client (OkHttp, kotlinx.serialization, coroutines) and also runs on the JVM. The optional byotalk-android module adds SharedPreferencesPersistence and attachLifecycle(), which reconnects when the app returns to the foreground or the network comes back. Your app needs the INTERNET permission.

2. Connect and send (development)

Development environments accept dev tokens, so you can start without a backend.

val chat = ByoTalkChat(env = "env_dev_2f8k", token = ByoTalkChat.devToken("alice"))
chat.connect()
val dm = chat.conversations.direct("bob")

lifecycleScope.launch {
    dm.events.collect { e ->
        if (e is ConversationEvent.MessageNew) println("${e.message.senderId}: ${e.message.text}")
    }
}
dm.send(text = "Hello Bob")

connect() returns once the connection is up and missed events are synced. To show a message list, collect dm.messages.items: a StateFlow<List<Message>> ordered by seq, without duplicates, with your pending sends at the end.

3. Production

Dev tokens are always disabled in production. Add a token route to your backend (see Quick start § 4) and pass a suspend function that fetches a token. It is called again before the token expires and after the server reports token_expired.

val chat = ByoTalkChat(
    env = "env_2f8k",
    tokenProvider = { api.fetchChatToken() },
    persistence = SharedPreferencesPersistence(context),
)
val detach = chat.attachLifecycle(application)

Never put a secret key (sk_...) in an app: the constructor refuses it. On sign-out call chat.clearLocalData() and then chat.disconnect().

API overview

Call What it does
connect(), disconnect(), reconnectNow() chat.connection is a StateFlow: DISCONNECTED, CONNECTING, SYNCING, CONNECTED, RECONNECTING, FAILED
chat.errors SharedFlow<ChatException>; every error has code, message and, for REST errors, requestId
conversations.list(), conversations.watch() Summaries with unreadCount, lastMessage and metadata; watch() stays current
conversations.direct(userId), create(members, name), get(id) A Conversation with its latest page loaded
send(text), retry(clientMsgId), edit(id, text), delete(id) Sends survive going offline and are resent with the same clientMsgId
loadOlder(), hasOlder Page back through history
markRead(), readBy(seq), ConversationEvent.Receipt Read receipts; delivered receipts are automatic
typing(), stopTyping(), typingUserIds Typing indicators (expire after 6 s)
presence.watch(userIds) Flow<Presence>: online status and last seen

Not included: calls, attachment uploads and push registration (use POST /v1/push/devices from the REST API).