iOS and Swift

A native Swift client for iOS 15+ and macOS 12+. 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 Swift concurrency throughout: async/await for calls and AsyncStream for connection state, message lists and events. It has no dependencies beyond Foundation and Network.

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

1. Install

Add the package with Swift Package Manager. The package lives in the swift/ folder of the SDK repository:

// Package.swift
dependencies: [
    .package(url: "https://github.com/devicorn/byotalk-sdk.git", branch: "main"),
],
targets: [
    .target(name: "MyApp", dependencies: [.product(name: "ByoTalk", package: "byotalk-sdk")]),
]

In Xcode: File → Add Package Dependencies…, paste the repository URL and add the ByoTalk library.

2. Connect and send (development)

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

import ByoTalk

let chat = try ByoTalkChat(env: "env_dev_2f8k", token: .token(ByoTalkChat.devToken("alice")))
try await chat.connect()
let dm = try await chat.conversations.direct("bob")

Task {
    for await event in dm.events() {
        if case .messageNew(let m) = event { print(m.senderId, m.text ?? "") }
    }
}
try await dm.send(text: "Hello Bob")

connect() returns once the connection is up and missed events are synced. To show a message list, iterate dm.messages.updates(): the current list first, then a new list after every change, ordered by seq, without duplicates, with your pending sends at the end. Use message.key as the row identity: it stays the same when a pending message is confirmed.

The client, conversations and message stores are @MainActor, so you can read them straight from SwiftUI.

3. Production

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

let chat = try ByoTalkChat(
    env: "env_2f8k",
    token: .provider { try await api.fetchChatToken() },
    persistence: UserDefaultsPersistence()
)

With UserDefaultsPersistence unsent messages survive an app restart (resent within 24 hours); the default keeps them in memory only. You can pass your own PersistenceAdapter (Keychain, a file, a database). Never put a secret key (sk_...) in an app: the initializer refuses it. On sign-out call await chat.clearLocalData() and then chat.disconnect().

The SDK reconnects at once when the network comes back or the app returns to the foreground.

API overview

Call What it does
connect(), disconnect(), reconnectNow() connectionStates() streams disconnected, connecting, syncing, connected, reconnecting, failed
chat.errors() AsyncStream<ChatError>; 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(name:members:metadata:), 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
update(name:metadata:), addMembers, removeMember, leave, mute Group management; name and metadata changes reach everyone as .updated
markRead(), readBy(seq), .receipt Read receipts; delivered receipts are automatic
typing(), stopTyping(), typingUserIds Typing indicators (expire after 6 s)
presence.watch(userIds) AsyncStream<Presence>: online status and last seen
push.register(token:) Registers the device's APNs token for offline notifications

Not included: calls and attachment uploads (messages can reference attachment ids uploaded through the REST API).