Product update · · 2 min read

Build custom iOS chat with the Swift Data API

Read, send, and subscribe to chat data from Swift and build your own chat UI.

Swift

A chat screen in a native iOS app should look like the rest of your app. To build one in SwiftUI or UIKit, you need the chat data itself: conversations, messages, and changes as they happen.

The TalkJS Swift Data API gives iOS and macOS apps direct access to TalkJS chat data. Install the talkjs-core-swift package with Swift Package Manager, connect as a user, and read, send, and subscribe to conversations, messages, participants, and users in real time.

It supports iOS 15+ and macOS 11+, and needs Swift 6.3 or later.

Subscribe to messages with the Swift Data API

The following example connects as the user alice, prints new messages in a conversation, and sends a reply:

import talkjs_core_swift

let session = getTalkSession(appId: "<APP_ID>", userId: "alice")
let conversation = session.conversation(id: "support-alice")

let subscription = conversation.subscribeMessages { messages, loadedAll in
  for message in messages ?? [] {
    print("\(message.sender?.name ?? "System"): \(message.plaintext)")
  }
}

_ = await conversation.send(text: "Hi, I have a question about my order.")

Replace <APP_ID> with your own TalkJS app ID, which you can find on the Settings page of your TalkJS Dashboard.

subscribeMessages calls your closure with the 10 most recent messages, then again whenever a message arrives or someone edits one. Call loadMore on the subscription to load older messages, and unsubscribe when your view goes away. The snapshot is nil if the user isn't a participant in the conversation. send(text:) is an async function, so call it from a Task or another async context.

In production, pass a token or tokenFetcher to getTalkSession to authenticate the user.

What you can build with the Swift Data API

The same subscribe-and-update pattern covers the rest of a chat app:

TalkJS Data APIs for iOS and Android

The Swift Data API shares its core with the Kotlin Data API, so getTalkSession, conversation(id:), and subscribeMessages work similarly on iOS and Android. If your team ships both apps, your chat logic follows the same model on both platforms.

Swift Data API FAQ

Does the Swift Data API include a chat UI?

No. The Swift Data API gives you chat data and real-time updates, and you build the screens in SwiftUI or UIKit.

Can I use the Swift Data API on a server?

No. The Swift Data API connects as one user, so it's meant for client apps. For server-side access to all users and conversations, use the REST API.

Can I add TalkJS chat to a React Native iOS app?

Yes. Use the React Native UI Components for a ready-made chat UI, or the JavaScript Data API, which supports React Native, to build your own.

Get started with the Swift Data API

You can add the package to your project using Swift Package Manager. In Xcode, get started by choosing File > Add Package Dependencies... and enter https://github.com/talkjs/talkjs-core-swift. The Swift Data API docs cover full installation steps, along with reference details for sessions, conversations, messages, participants, and users.