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.

interface UserRef

References the user with a given user ID.

Used in all Data API operations affecting that user, such as creating the user, fetching or updating user data, or adding a user to a conversation. Created via Session​.user.

Method Overview

createIfNotExists

Creates a user with this ID, or does nothing if a user with this ID already exists.

get

Fetches a snapshot of the user.

set

Sets properties of this user. The user is created if a user with this ID doesn't already exist.

subscribe

Subscribe to this user's state.

Properties

id
: string

The ID of the referenced user.

Immutable: if you want to reference a different user, get a new UserRef instead.

createIfNotExists

userRef.createIfNotExists(params): Promise<void>

Creates a user with this ID, or does nothing if a user with this ID already exists.

If the user already exists, this operation is still considered successful and the promise will still resolve.

Parameters

interface CreateUserParams

Parameters you can pass to UserRef​.createIfNotExists.

Properties that are undefined will be set to the default.

name
: string

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

custom (optional)
: Record<string, string>

Custom metadata you have set on the user. Default = no custom metadata

email (optional)
: string | string[]

A single email address or an array of email addresses associated with the user. Default = no email addresses

locale (optional)
: string

An IETF language tag. See the localization documentation Default = the locale selected on the dashboard

phone (optional)
: string | string[]

A single phone number or an array of phone numbers associated with the user. Default = no phone numbers

photoUrl (optional)
: string

An optional URL to a photo that is displayed as the user's avatar. Default = no photo

pushTokens (optional)
: Record<string, true>

An object of push registration tokens to use when notifying this user.

Keys in the object have the format 'provider:token_id', where provider is either "fcm" for Android (Firebase Cloud Messaging), or "apns" for iOS (Apple Push Notification Service).

Default = no push registration tokens

role (optional)
: string

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

welcomeMessage (optional)
: string

The default message a person sees when starting a chat with this user. Default = no welcome message

Returns

Promise<void>

A promise that resolves when the operation completes. The promise will reject if client-side user syncing is disabled and the user does not already exist.

get

userRef.get(): Promise<UserSnapshot | null>

Fetches a snapshot of the user.

This contains all of a user's public information. Fetching a user snapshot doesn't require any permissions. You can read the public information of any user. Private information, such as email addresses and phone numbers, aren't included in the response.

Supports Automatic Batching and Cached Fetch

Returns

Promise<UserSnapshot | null>

A snapshot of the user's public attributes, or null if the user doesn't exist.

set

userRef.set(params): Promise<void>

Sets properties of this user. The user is created if a user with this ID doesn't already exist.

name is required when creating a user. The promise will reject if you don't provide a name and the user does not exist yet.

Parameters

params
: SetUserParams
interface SetUserParams

Parameters you can pass to UserRef​.set.

Properties that are undefined will not be changed. To clear / reset a property to the default, pass null.

custom (optional)
: Record<string, string | null> | null

Custom metadata you have set on the user. This value acts as a patch. Remove specific properties by setting them to null. Default = no custom metadata

email (optional)
: string | string[] | null

A single email address or an array of email addresses associated with the user. Default = no email addresses

locale (optional)
: string

An IETF language tag. See the localization documentation Default = the locale selected on the dashboard

name (optional)
: string

The user's name which will be displayed on the TalkJS UI

phone (optional)
: string | string[] | null

A single phone number or an array of phone numbers associated with the user. Default = no phone numbers

photoUrl (optional)
: string | null

An optional URL to a photo which will be displayed as the user's avatar. Default = no photo

pushTokens (optional)
: Record<string, true | null> | null

An object of push registration tokens to use when notifying this user.

Keys in the object have the format 'provider:token_id', where provider is either "fcm" for Android (Firebase Cloud Messaging), or "apns" for iOS (Apple Push Notification Service).

The value for each key can either be true to register the device for push notifications, or null to unregister that device.

Setting pushTokens to null unregisters all the previously registered devices.

Default = no push tokens

role (optional)
: string | null

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

welcomeMessage (optional)
: string | null

The default message a person sees when starting a chat with this user. Default = no welcome message

Returns

Promise<void>

A promise that resolves when the operation completes. The promise will reject if the request is invalid or if client-side user syncing is disabled.

subscribe

userRef.subscribe(onSnapshot): UserSubscription

Subscribe to this user's state.

Whenever Subscription.state.type is "active" and the user is created or their attributes change, onSnapshot will fire and Subscription.state.latestSnapshot will be updated.

Parameters

onSnapshot (optional)
: (event: UserSnapshot | null) => void

Returns

UserSubscription

A subscription to the user

interface 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 REST API.

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

Properties

custom
: Record<string, string>

Custom metadata you have set on the user

id
: string

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

locale
: string | null

An IETF language tag. For more information, see: Localization.

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

name
: string

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

photoUrl
: string | null

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

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.

welcomeMessage
: string | null

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

interface UserSubscription

A subscription to a specific user

Get a UserSubscription by calling UserRef​.subscribe

Method Overview

unsubscribe

Unsubscribe from this resource and stop receiving updates.

Properties

connected
: Promise<UserActiveState>

Resolves when the subscription starts receiving updates from the server.

state
: PendingState | UserActiveState | UnsubscribedState | ErrorState

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.

terminated
: Promise<UnsubscribedState | ErrorState>

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

userSubscription.unsubscribe()

Unsubscribe from this resource and stop receiving updates.

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

Returns

void