Realtime API
The Realtime API provides access a continuous, up-to-date flow of your TalkJS data without the need to send repeated requests or open a chat UI.
Preview: Currently the Realtime API allows you to get / set user data, and get conversation data. The available methods are safe to use in production and will not change. Additional capabilities will be added to the API on a rolling basis.
Refs
These reference a specific resource in TalkJS. With a ref, you can get or set data on that resource.
Refs don't contain any actual data themselves, they just point to a TalkJS resource.
You can get the actual data (a snapshot) by calling .get()
on a ref.
References the user with a given user ID.
Used in all Realtime API operations affecting that user, such as fetching or updating user data, or adding a user to a conversation. Created via Session.user.
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. |
The ID of the referenced user.
Immutable: if you want to reference a different user, get a new UserRef instead.
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. This operation fails if client-side user syncing is disabled.
Parameters
Returns
A promise that resolves when the operation completes.
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.
Returns
A snapshot of the user's public attributes, or null if the user doesn't exist.
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. This operation fails if you don't provide a name
and the user does not exist yet. This operation also fails if client-side user syncing is disabled.
Parameters
Returns
A promise that resolves when the operation completes.
References the conversation with a given conversation ID, from the perspective of the current user.
Used in all Realtime API operations affecting that conversation, such as fetching or updating conversation attributes. Created via Session.conversation.
get | Fetches a snapshot of the conversation. |
The ID of the referenced conversation.
Immutable: if you want to reference a different conversation, get a new ConversationRef instead.
conversationRef.get(): Promise<ConversationSnapshot | null>
Fetches a snapshot of the conversation.
This contains all of the information related to the conversation and the current user's participation in the conversation. You must be a participant in the conversation.
Returns
A snapshot of the current user's view of the conversation, or null if the current user is not a participant (including if the conversation doesn't exist).
Snapshots
When you fetch data for a ref, you get a snapshot. Snapshots contains all the relevant information about a resource, at a specific point in time. If the data changes on the database, old snapshots will not be automatically updated.
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.
Custom metadata you have set on the user
The unique ID that is used to identify the user in TalkJS
An IETF language tag. For more information, see: Localization.
The user's name, which is displayed on the TalkJS UI
An optional URL to a photo that is displayed as the user's avatar
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.
The default message a person sees when starting a chat with this user
A snapshot of a conversation's attributes at a given moment in time.
Also includes information about the current user's view of that conversation, such as whether or not notifications are enabled.
The current user's permission level in this conversation.
The date that the conversation was created, as a unix timestamp in milliseconds.
Custom metadata you have set on the conversation
The ID of the conversation
Whether the conversation should be considered unread.
This can be true even when unreadMessageCount
is zero, if the user has manually marked the conversation as unread.
The date of the conversation's most recent message, as a unix timestamp in milliseconds.
If no messages have been sent, this will be the date that the conversation was created instead. To show a chronological list of recent conversations, sort by this property.
The current user's notification settings for this conversation.
false
means no notifications, true
means notifications for all messages, and "MentionsOnly"
means that the user will only be notified when they are mentioned with an @
.
Contains the URL of a photo was set using ConversationBuilder.subject.
Contains the conversation subject if it was set using ConversationBuilder.subject.
The number of messages in this conversation that the current user hasn't read.
One or more welcome messages that will display to the user as a SystemMessage
Other Interfaces
These interfaces are used when creating / updating resources.
Parameters you can pass when creating a user.
The user's name which is displayed on the TalkJS UI
Custom metadata you have set on the user. Default = no custom data
An array of email addresses associated with the user. Default = no email addresses
An IETF language tag. See the localization documentation Default = the locale selected on the dashboard
An array of phone numbers associated with the user. Default = no phone numbers
An optional URL to a photo that is displayed as the user's avatar. Default = no photo
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
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
The default message a person sees when starting a chat with this user. Default = no welcome message
Parameters you can pass when updating a user.
Properties that are undefined
will not be changed. To clear / reset a property to the default, pass null
.
Custom metadata you have set on the user. null
= no custom metadata
An array of email addresses associated with the user. null
= no email addresses
An IETF language tag. See the localization documentation null
= the locale selected on the dashboard
The user's name which will be displayed on the TalkJS UI
An array of phone numbers associated with the user. null
= no phone numbers
An optional URL to a photo which will be displayed as the user's avatar. null
= no photo
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.
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. null
= the default
role
The default message a person sees when starting a chat with this user. null
= no welcome message