---
url: https://talkjs.com/docs/Data_APIs/Swift/TalkSession/
title: "TalkSession | Swift | TalkJS Documentation"
---

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.

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

### getTalkSession

func getTalkSession(appId:userId:token:tokenFetcher:) -> [Session](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session)

## struct Session

### Method Overview

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__conversation" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">conversation</a></td><td><p>Get a reference to a conversation</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__onError" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">onError</a></td><td><p>Attaches a handler that will be called when the session encounters an error</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__subscribeConversations" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">subscribeConversations</a></td><td><p>Subscribes to the most recently active conversations for the current user</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadAudio" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">uploadAudio</a></td><td><p>Upload an audio file with audio-specific metadata.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadFile" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">uploadFile</a></td><td><p>Upload a generic file without any additional metadata.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadImage" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">uploadImage</a></td><td><p>Upload an image with image-specific metadata.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadVideo" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">uploadVideo</a></td><td><p>Upload a video with video-specific metadata.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadVoice" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">uploadVoice</a></td><td><p>Upload a voice recording with voice-specific metadata.</p></td></tr><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__user" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">user</a></td><td><p>Get a reference to a user</p></td></tr></tbody></table>

### Properties

**currentUser**: [UserRef](https://talkjs.com/docs/Data_APIs/Swift/Users/#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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#getTalkSession) again to get a new session.

Equivalent to calling [Session.user](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__user) with the current user’s ID.

[Session.user](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__user) which lets you get a reference to any user.

### conversation

func _session_.conversation(id:) -> [ConversationRef](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationRef)

Get a reference to a conversation

#### Parameters

**id**: String

The ID of the conversation that you want to reference

#### Returns

[ConversationRef](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationRef)

A [ConversationRef](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationRef) for the conversation with that ID

### onError

func _session_.onError(handler:) -> any [Subscription](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Subscription)

### subscribeConversations

func _session_.subscribeConversations(onSnapshot:) -> [ConversationListSubscription](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationListSubscription)

Subscribes to the most recently active conversations for the current user

#### Parameters

**onSnapshot _(optional)_**: (@Sendable (\_ snapshot: \[[ConversationSnapshot](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationSnapshot)\], \_ loadedAll: Bool) -> Void)?

#### Returns

[ConversationListSubscription](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#ConversationListSubscription)

### uploadAudio

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

Upload an audio file with audio-specific metadata.

This is a variant of [Session.uploadFile](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadFile) used for audio files.

#### Parameters

**data**: \[Int8\]

The binary audio data. Usually a File.

**metadata**: [AudioFileMetadata](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#AudioFileMetadata)

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](https://talkjs.com/docs/Data_APIs/Swift/Message_Content/#SendFileBlock) when calling [ConversationRef.send](https://talkjs.com/docs/Data_APIs/Swift/Conversations/#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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadImage) instead.

#### Parameters

**data**: \[Int8\]

The binary file data. Usually a File.

**metadata**: [GenericFileMetadata](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#GenericFileMetadata)

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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadFile) used for images.

#### Parameters

**data**: \[Int8\]

The binary image data. Usually a File.

**metadata**: [ImageFileMetadata](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#ImageFileMetadata)

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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadFile) used for videos.

#### Parameters

**data**: \[Int8\]

The binary video data. Usually a File.

**metadata**: [VideoFileMetadata](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#VideoFileMetadata)

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](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#Session__uploadFile) used for voice recordings.

#### Parameters

**data**: \[Int8\]

The binary audio data. Usually a File.

**metadata**: [VoiceRecordingFileMetadata](https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#VoiceRecordingFileMetadata)

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](https://talkjs.com/docs/Data_APIs/Swift/Users/#UserRef)

Get a reference to a user

#### Parameters

**id**: String

The ID of the user that you want to reference

#### Returns

[UserRef](https://talkjs.com/docs/Data_APIs/Swift/Users/#UserRef)

A [UserRef](https://talkjs.com/docs/Data_APIs/Swift/Users/#UserRef) for the user with that ID

## protocol SubscriptionState

No properties or methods are currently defined.

## struct GenericFileMetadata

### Method Overview

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#GenericFileMetadata__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr></tbody></table>

### constructor

GenericFileMetadata(filename:)

#### Parameters

**filename**: String

The name of the file including extension.

## struct ImageFileMetadata

### Method Overview

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#ImageFileMetadata__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr></tbody></table>

### 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

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#VideoFileMetadata__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr></tbody></table>

### 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

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#AudioFileMetadata__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr></tbody></table>

### 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

<table class="my-0"><tbody><tr class="flex flex-col flex-no-wrap sm:table-row"><td><a href="https://talkjs.com/docs/Data_APIs/Swift/TalkSession/#VoiceRecordingFileMetadata__constructor" class="no-underline font-mono text-blue-500 hover:text-blue-600 font-medium">constructor</a></td><td></td></tr></tbody></table>

### 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.
