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.

TalkSession

Create and manage a session for the Swift Data API.

View as Markdown

This is the entry point to the TalkJS Swift Data API.

getTalkSession

func getTalkSession(appId:userId:token:tokenFetcher:) -> Session

Returns a TalkSession option for the specified App ID and User ID.

Backed by a registry, so calling this function twice with the same app and user returns the same session object both times. A new session will be created if the old one encountered an error or got garbage collected.

The token and tokenFetcher properties are ignored if there is already a session for that user in the registry.

Parameters

appId
: String

Your app’s unique TalkJS ID. Get it from the Settings page of the dashboard.

userId
: String

The id of the user you want to connect and act as. Any messages you send will be sent as this user.

token (optional)
: String?

A token to authenticate the session with. Ignored if a TalkSession object already exists for this appId + userId.

tokenFetcher (optional)
: TokenFetcher?

A callback that fetches a new token from your backend and returns it. If this callback throws an error, the session will terminate. Your callback should retry failed requests. Ignored if a TalkSession object already exists for this appId + userId.

Returns

Session

struct Session

Method Overview

conversation

Get a reference to a conversation

onError

Attaches a handler that will be called when the session encounters an error

subscribeConversations

Subscribes to the most recently active conversations for the current user

uploadAudio

Upload an audio file with audio-specific metadata.

uploadFile

Upload a generic file without any additional metadata.

uploadImage

Upload an image with image-specific metadata.

uploadVideo

Upload a video with video-specific metadata.

uploadVoice

Upload a voice recording with voice-specific metadata.

user

Get a reference to a user

Properties

currentUser
: UserRef

A reference to the user this session is connected as

This is immutable. If you want to connect as a different user, call getTalkSession again to get a new session.

Equivalent to calling Session.user with the current user’s ID.

Session.user which lets you get a reference to any user.

conversation

func session.conversation(id:) -> ConversationRef

Get a reference to a conversation

Parameters

id
: String

The ID of the conversation that you want to reference

Returns

ConversationRef

A ConversationRef for the conversation with that ID

onError

func session.onError(handler:) -> any Subscription

Attaches a handler that will be called when the session encounters an error

Returns a callback which detaches your handler

Parameters

handler
: @escaping @Sendable (_ error: TalkJSError) -> Void

Returns

any Subscription

subscribeConversations

func session.subscribeConversations(onSnapshot:) -> ConversationListSubscription

Subscribes to the most recently active conversations for the current user

Parameters

onSnapshot (optional)
: (@Sendable (_ snapshot: [ConversationSnapshot], _ loadedAll: Bool) -> Void)?

Returns

ConversationListSubscription

uploadAudio

func session.uploadAudio(data:metadata:) async -> String

Upload an audio file with audio-specific metadata.

This is a variant of Session.uploadFile used for audio files.

Parameters

data
: [Int8]

The binary audio data. Usually a File.

Information about the audio file.

Returns

String

A file token that can be used to send the audio file in a message.

uploadFile

func session.uploadFile(data:metadata:) async -> String

Upload a generic file without any additional metadata.

This function does not send any message, it only uploads the file and returns a file token. To send the file in a message, pass the file token in a SendFileBlock when calling ConversationRef.send.

See the documentation for more information about sending files in messages.

If the file is a video, image, audio file, or voice recording, use one of the other functions like Session.uploadImage instead.

Parameters

data
: [Int8]

The binary file data. Usually a File.

Information about the file

Returns

String

A file token that can be used to send the file in a message.

uploadImage

func session.uploadImage(data:metadata:) async -> String

Upload an image with image-specific metadata.

This is a variant of Session.uploadFile used for images.

Parameters

data
: [Int8]

The binary image data. Usually a File.

Information about the image.

Returns

String

A file token that can be used to send the image in a message.

uploadVideo

func session.uploadVideo(data:metadata:) async -> String

Upload a video with video-specific metadata.

This is a variant of Session.uploadFile used for videos.

Parameters

data
: [Int8]

The binary video data. Usually a File.

Information about the video.

Returns

String

A file token that can be used to send the video in a message.

uploadVoice

func session.uploadVoice(data:metadata:) async -> String

Upload a voice recording with voice-specific metadata.

This is a variant of Session.uploadFile used for voice recordings.

Parameters

data
: [Int8]

The binary audio data. Usually a File.

Information about the voice recording.

Returns

String

A file token that can be used to send the audio file in a message.

user

func session.user(id:) -> UserRef

Get a reference to a user

Parameters

id
: String

The ID of the user that you want to reference

Returns

UserRef

A UserRef for the user with that ID

protocol SubscriptionState

No properties or methods are currently defined.

struct GenericFileMetadata

Method Overview

constructor

GenericFileMetadata(filename:)

Parameters

filename
: String

The name of the file including extension.

struct ImageFileMetadata

Method Overview

constructor

ImageFileMetadata(filename:width:height:)

Parameters

filename
: String

The name of the file including extension.

width (optional)
: Int?

The width of the image in pixels, if known.

height (optional)
: Int?

The height of the image in pixels, if known.

struct VideoFileMetadata

Method Overview

constructor

VideoFileMetadata(filename:duration:width:height:)

Parameters

filename
: String

The name of the file including extension.

duration (optional)
: Double?

The duration of the video in seconds, if known.

width (optional)
: Int?

The width of the video in pixels, if known.

height (optional)
: Int?

The height of the video in pixels, if known.

struct AudioFileMetadata

Method Overview

constructor

AudioFileMetadata(filename:duration:)

Parameters

filename
: String

The name of the file including extension.

duration (optional)
: Double?

The duration of the audio file in seconds, if known.

struct VoiceRecordingFileMetadata

Method Overview

constructor

VoiceRecordingFileMetadata(filename:duration:)

Parameters

filename
: String

The name of the file including extension.

duration (optional)
: Double?

The duration of the recording in seconds, if known.