Users

A user is someone who can send and receive messages. Usually, they will map directly to the accounts in your website or app. However, you can also create users to act as bots or for AI agents.

class UserRef

Method Overview

Properties

id
: String

get

userRef.get(): UserSnapshot?

Returns

UserSnapshot?

set

userRef.set(data): Unit

Parameters

Returns

Unit

deleteFields

userRef.deleteFields(fields): Unit

Parameters

fields
: String

Returns

Unit

createIfNotExists

userRef.createIfNotExists(data): Unit

Parameters

Returns

Unit

subscribe

userRef.subscribe(onSnapshot): UserSubscription

Parameters

onSnapshot (optional)
: (UserSnapshot?) -> Unit?

Returns

UserSubscription

subscribeOnline

userRef.subscribeOnline(onSnapshot): UserOnlineSubscription

Parameters

onSnapshot (optional)
: (UserOnlineSnapshot?) -> Unit?

Returns

UserOnlineSubscription

constructor

new UserRef.constructor(id, _realtimeClient)

Parameters

id
: String
_realtimeClient
: RealtimeClient

data class UserSnapshot

A snapshot of a user's attributes at a given moment in time.

Users also have private information, such as email addresses and phone numbers, but these are only exposed on the

Method Overview

Properties

id
: String

The unique ID that is used to identify the user in TalkJS

name
: String

The user's name, which is displayed on the TalkJS UI

custom
: Map<String, String>

Custom metadata you have set on the user

role
: String

TalkJS supports multiple sets of settings for users, called "roles". Roles allow you to change the behavior of TalkJS for different users. You have full control over which user gets which configuration.

locale (optional)
: String?

An For more information, see:

When locale is null, the app's default locale will be used

photoUrl (optional)
: String?

An optional URL to a photo that is displayed as the user's avatar

welcomeMessage (optional)
: String?

The default message a person sees when starting a chat with this user

constructor

new UserSnapshot.constructor(id, name, custom, role, locale, photoUrl, welcomeMessage)

Parameters

id
: String
name
: String
custom
: Map<String, String>
role
: String
locale (optional)
: String?
photoUrl (optional)
: String?
welcomeMessage (optional)
: String?

data class UserSubscription

Method Overview

Properties

state
: SubscriptionState

The current state of the subscription

An object with the following fields:

type is one of "pending", "active", "unsubscribed", or "error".

When type is "active", includes latestSnapshot: UserSnapshot | null. It is the current state of the user, or null if they don't exist.

When type is "error", includes the error: Error field. It is a JS Error object explaining what caused the subscription to be terminated.

connected
: Deferred<SubscriptionState>

Resolves when the subscription starts receiving updates from the server.

terminated
: Deferred<SubscriptionState>

Resolves when the subscription permanently stops receiving updates from the server.

This is either because you unsubscribed or because the subscription encountered an unrecoverable error.

unsubscribe
: () -> Unit

Unsubscribe from this resource and stop receiving updates.

If the subscription is already in the "unsubscribed" or "error" state, this is a no-op.

loadMore
: suspend (Int?) -> Unit
error (optional)
: Exception?

constructor

new UserSubscription.constructor(state, connected, terminated, unsubscribe)

Parameters

state
: SubscriptionState
connected
: Deferred<SubscriptionState>
terminated
: Deferred<SubscriptionState>
unsubscribe
: () -> Unit

data class UserOnlineSnapshot

A snapshot of a user's online status at a given moment in time.

Snapshots are immutable and we try to reuse them when possible. You should only re-render your UI when oldSnapshot !== newSnapshot.

Method Overview

Properties

The user this snapshot relates to

isConnected
: Boolean

Whether the user is connected right now

Users are considered connected whenever they have an active websocket connection to the TalkJS servers. In practice, this means:

People using the {@link https://talkjs.com/docs/Reference/JavaScript_Data_API/ | JS Data API} are considered connected if they are subscribed to something, or if they sent a request in the last few seconds. Creating a TalkSession is not enough to appear connected.

People using {@link https://talkjs.com/docs/Reference/Components/ | Components}, are considered connected if they have a UI open.

People using the {@link https://talkjs.com/docs/Reference/JavaScript_Chat_SDK/ | JavaScript SDK}, {@link https://talkjs.com/docs/Reference/React_SDK/Installation/ | React SDK}, {@link https://talkjs.com/docs/Reference/React_Native_SDK/Installation/ | React Native SDK}, or {@link https://talkjs.com/docs/Reference/Flutter_SDK/Installation/ | Flutter SDK} are considered connected whenever they have an active Session object.

constructor

new UserOnlineSnapshot.constructor(user, isConnected)

Parameters

isConnected
: Boolean

data class UserOnlineSubscription

A subscription to the online status of a user

Get a UserOnlineSubscription by calling {@link UserRef.subscribeOnline}.

Remember to .unsubscribe the subscription once you are done with it.

Method Overview

Properties

state
: SubscriptionState

The current state of the subscription

An object with the following fields:

type is one of "pending", "active", "unsubscribed", or "error".

When type is "active", includes latestSnapshot: UserOnlineSnapshot | null null means the user does not exist.

When type is "error", includes the error field. It is a JS Error object explaining what caused the subscription to be terminated.

connected
: Deferred<UserOnlineActiveState>

Resolves when the subscription starts receiving updates from the server.

Wait for this promise if you want to perform some action as soon as the subscription is active.

The promise rejects if the subscription is terminated before it connects.

terminated
: Deferred<SubscriptionState>

Resolves when the subscription permanently stops receiving updates from the server.

This is either because you unsubscribed or because the subscription encountered an unrecoverable error.

unsubscribe
: () -> Unit

Unsubscribe from this resource and stop receiving updates.

If the subscription is already in the "unsubscribed" or "error" state, this is a no-op.

loadMore
: suspend (Int?) -> Unit
error (optional)
: Exception?

constructor

new UserOnlineSubscription.constructor(state, connected, terminated, unsubscribe)

Parameters

state
: SubscriptionState
connected
: Deferred<UserOnlineActiveState>
terminated
: Deferred<SubscriptionState>
unsubscribe
: () -> Unit