For AI agents: Find a documentation index at https://talkjs.com/llms.txt (full content at https://talkjs.com/llms-full.txt). Get a markdown version of any page by appending .md to its URL path.

Swift Data API

Connect to chat data as a user.

View as Markdown

The Swift Data API lets you connect to TalkJS as a user and read, subscribe to, and update chat data from Swift-based environments. It's available through the TalkJS Core (talkjs-core-swift) Swift package on GitHub.

The Data API is primarily intended for client environments such as iOS apps, because it connects as a single specific user.

If you need server-side access to all conversations or users, use the REST API instead.

Requirements

To use the Swift Data API, your project needs to have the following:

  • Swift 6.3+
  • For iOS apps: iOS 15+
  • For macOS apps: macOS 11+

Installation

To add the TalkJS Core package to your project using Swift Package Manager:

  • In Xcode, choose File > Add Package Dependencies..., enter https://github.com/talkjs/talkjs-core-swift, and add the talkjs-core-swift library to your app target.
  • If you use a Package.swift file, add the package as a dependency and the talkjs-core-swift library to your target:
1dependencies: [
2 .package(url: "https://github.com/talkjs/talkjs-core-swift", from: "<latest_version>"),
3],
4targets: [
5 .target(
6 name: "<YOUR_TARGET>",
7 dependencies: [
8 .product(name: "talkjs-core-swift", package: "talkjs-core-swift"),
9 ]
10 ),
11]

Replace <latest_version> with the latest available version, for example 0.0.3. You can check the GitHub tags for the latest version.

Create a TalkJS session

To use the Swift Data API, import the talkjs_core_swift module.

To connect as a user, create a TalkJS session using getTalkSession:

1import talkjs_core_swift
2
3let session = getTalkSession(
4 appId: "<APP_ID>", // Replace with your own app ID
5 userId: "<USER_ID>" // Replace with a user ID
6)

Replace:

  • <APP_ID> with your TalkJS app ID. You can find your app ID on the Settings page of your TalkJS dashboard.
  • <USER_ID> with the ID of the user you want to connect as.

Once connected, the session allows you to retrieve conversations, send messages, and subscribe to real-time updates.