Conversations
A conversation is a chat channel where messages can be sent. Users can participate in many conversations at once.
References the conversation with a given conversation ID, from the perspective of the current user.
Used in all Data API operations affecting that conversation, such as fetching or updating conversation attributes. Created via Session.conversation().
createIfNotExists | Creates this conversation if it does not already exist. Adds you as a participant in this conversation, if you are not already a participant. |
get | Fetches a snapshot of the conversation. |
markAsRead | Marks the conversation as read. |
markAsTyping | Marks the current user as typing in this conversation for 10 seconds. |
markAsUnread | Marks the conversation as unread. |
message | Get a reference to a message in this conversation |
participant | Get a reference to a participant in this conversation |
send | Sends a message in the conversation |
set | Sets properties of this conversation and your participation in it. |
subscribe | Subscribes to the conversation. |
subscribeMessages | Subscribes to the messages in the conversation. |
subscribeTyping | Subscribes to the typing status of the conversation. |
The ID of the referenced conversation.
Immutable: if you want to reference a different conversation, get a new ConversationRef instead.
conversationRef.createIfNotExists(params): Promise<void>
Creates this conversation if it does not already exist. Adds you as a participant in this conversation, if you are not already a participant.
If the conversation already exists or you are already a participant, this operation is still considered successful and the promise will still resolve.
Parameters
interface CreateConversationParams
Parameters you can pass to ConversationRef.createIfNotExists.
Properties that are undefined
will be set to the default.
Your access to the conversation. Default = "ReadWrite" access.
Custom metadata you have set on the conversation. This value acts as a patch. Remove specific properties by setting them to null
. Default = no custom metadata
Your notification settings. Default = true
The URL for the conversation photo to display in the chat header. Default = no photo, show a placeholder image.
The conversation subject to display in the chat header. Default = no subject, list participant names instead.
System messages which are sent at the beginning of a conversation. Default = no messages.
Returns
A promise that resolves when the operation completes. The promise rejects if you are not already a participant and client-side conversation syncing is disabled.
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.
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).
conversationRef.markAsRead(): Promise<void>
Marks the conversation as read.
Returns
A promise that resolves when the operation completes. The promise rejects if you are not a participant in the conversation.
conversationRef.markAsTyping(): Promise<void>
Marks the current user as typing in this conversation for 10 seconds.
This means that other users will see a typing indicator in the UI, from the current user.
The user will automatically stop typing after 10 seconds. You cannot manually mark a user as "not typing". Users are also considered "not typing" when they send a message, even if that message was sent from a different tab or using the REST API.
To keep the typing indicator visible for longer, call this function again to reset the 10s timer.
Returns
Usage
1let lastMarkedAsTyping = 0;23inputElement.addEventListener("change", event => {4 const text = event.target.value;56 // Deleting your draft never counts as typing7 if (text.length === 0) {8 return;9 }1011 const now = Date.now();1213 // Don't mark as typing more than once every 5s14 if (now - lastMarkedAsTyping > 5000) {15 lastMarkedAsTyping = now;16 convRef.markAsTyping();17 }18});1920// When you send a message, you are no longer considered typing21// So we need to send markAsTyping as soon as you type something22function onSendMessage() {23 lastMarkedAsTyping = 0;24}
conversationRef.markAsUnread(): Promise<void>
Marks the conversation as unread.
Returns
A promise that resolves when the operation completes. The promise rejects if you are not a participant in the conversation.
conversationRef.message(id): MessageRef
Get a reference to a message in this conversation
Use this if you need to fetch, delete, or edit a specific message in this conversation, and you know its message ID. To fetch the most recent messages in this conversation, use subscribeMessages instead.
Parameters
The ID of the user that you want to reference
Returns
A for the user with that ID
conversationRef.participant(user): ParticipantRef
Get a reference to a participant in this conversation
Note that Participant
is not the same as User
. A Participant
represents a user's settings related to a specific conversation.
Calling ConversationRef.createIfNotExists or ConversationRef.set will automatically add the current user as a participant.
Parameters
Specifies which participant in the conversation you want to reference. Either the user's ID, or a reference to that user.
Returns
A for that user's participation in this conversation
Usage
1session.conversation("Cats").participant("Alice").createIfNotExists();
The user "Alice" must exist before you do this.
conversationRef.send(params): Promise<MessageRef>
Sends a message in the conversation
Parameters
interface SendTextMessageParams
Parameters you can pass to ConversationRef.send.
Properties that are undefined
will be set to the default.
This is a simpler version of SendMessageParams that only supports text messages.
The text to send in the message.
This is parsed the same way as the text entered in the message field. For example, *hi*
will appear as hi
in bold.
See the message formatting documentation for more details.
Custom metadata you have set on the user. Default = no custom metadata
The message that you are replying to. Default = not a reply
interface SendMessageParams
Parameters you can pass to ConversationRef.send.
Properties that are undefined
will be set to the default.
This is the more advanced method for editing a message, giving full control over the message content. You can decide exactly how a text message should be formatted, edit an attachment, or even turn a text message into a location.
The most important part of the message, either some text, a file attachment, or a location.
By default users do not have permission to send LinkNode, ActionLinkNode, or ActionButtonNode, as they can be used to trick the recipient.
Currently, each message can only contain a single SendContentBlock.
Custom metadata you have set on the user. Default = no custom metadata
The message that you are replying to. Default = not a reply
Returns
A promise that resolves with a reference to the newly created message. The promise will reject if you do not have permission to send the message.
Usage
1conversationRef.send("*Hello*");
1conversationRef.send({2 text: "Agreed",3 referencedMessageId: "...",4 custom: { priority: "HIGH" }5});
1conversationRef.send({2 content: [{3 type: "text",4 children: [{5 type: "bold",6 children: ["Hello"]7 }]8 }]9});
1// `file` is a File object from `<input type="file">`2const fileToken = await session.uploadImage(3 file, { filename: file.name, width: 640, height: 480 }4);56conversationRef.send({7 content: [{ type: "file", fileToken }]8});
1// You can get the user's location with the browser's geolocation API2const [latitude, longitude] = [42.43, -83.99];3conversationRef.send({4 content: [{ type: "location", latitude, longitude }]5});
conversationRef.set(params): Promise<void>
Sets properties of this conversation and your participation in it.
The conversation is created if a conversation with this ID doesn't already exist. You are added as a participant if you are not already a participant in the conversation.
Parameters
interface SetConversationParams
Parameters you can pass to ConversationRef.set.
Properties that are undefined
will not be changed. To clear / reset a property to the default, pass null
.
Your access to the conversation. Default = "ReadWrite" access.
Custom metadata you have set on the conversation. This value acts as a patch. Remove specific properties by setting them to null
. Default = no custom metadata
Your notification settings. Default = true
The URL for the conversation photo to display in the chat header. Default = no photo, show a placeholder image.
The conversation subject to display in the chat header. Default = no subject, list participant names instead.
System messages which are sent at the beginning of a conversation. Default = no messages.
Returns
A promise that resolves when the operation completes. When client-side conversation syncing is disabled, you must already be a participant and you cannot set anything except the notify
property. Everything else requires client-side conversation syncing to be enabled, and will cause the promise to reject.
conversationRef.subscribe(onSnapshot): ConversationSubscription
Subscribes to the conversation.
Whenever Subscription.state.type
is "active" and something about the conversation changes, onSnapshot
will fire and Subscription.state.latestSnapshot
will be updated. This includes changes to nested data. As an extreme example, onSnapshot
would be called when snapshot.lastMessage.referencedMessage.sender.name
changes.
The snapshot is null if you are not a participant in the conversation (including when the conversation doesn't exist)
Parameters
Returns
conversationRef.subscribeMessages(onSnapshot): MessageSubscription
Subscribes to the messages in the conversation.
Initially, you will be subscribed to the 30 most recent messages and any new messages. Call loadMore
to load additional older messages.
Whenever Subscription.state.type
is "active" and a message is sent, edited, deleted, or you load more messages, onSnapshot
will fire and Subscription.state.latestSnapshot
will be updated. loadedAll
is true when the snapshot contains all the messages in the conversation.
The snapshot is null if you are not a participant in the conversation (including when the conversation doesn't exist)
Parameters
Returns
conversationRef.subscribeTyping(onSnapshot): TypingSubscription
Subscribes to the typing status of the conversation.
Whenever Subscription.state.type
is "active" and the typing status changes, onSnapshot
will fire and Subscription.state.latestSnapshot
will be updated. This includes changes to nested data, such as when a user who is typing changes their name.
The snapshot is null if you are not a participant in the conversation (including when the conversation doesn't exist)
Note that if there are "many" people typing and another person starts to type, onSnapshot
will not be called. This is because your existing ManyTypingSnapshot is still valid and did not change when the new person started to type.
Parameters
Returns
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.
Snapshots are immutable and we try to reuse them when possible. You should only re-render your UI when oldSnapshot !== newSnapshot
.
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 that the current user joined the conversation, as a unix timestamp in milliseconds.
The last message sent in this conversation, or null if no messages have been sent.
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 to represent the topic of the conversation or null
if the conversation does not have a photo specified.
Timestamp of when the current user read a message
Contains the conversation subject, or null
if the conversation does not have a subject specified.
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
A subscription to a specific conversation.
Get a ConversationSubscription by calling ConversationRef.subscribe
unsubscribe | Unsubscribe from this resource and stop receiving updates. |
Resolves when the subscription starts receiving updates from the server.
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: ConversationSnapshot | null
, the current state of the conversation. latestSnapshot
is null
when you are not a participant or the conversation 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.
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.
conversationSubscription.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
A subscription to the messages in a specific conversation.
Get a MessageSubscription by calling ConversationRef.subscribeMessages
The subscription is 'windowed'. It includes all messages since a certain point in time. By default, you subscribe to the 30 most recent messages, and any new messages are sent after you subscribe.
You can expand this window by calling loadMore, which extends the window further into the past.
loadMore | Expand the window to include older messages |
unsubscribe | Unsubscribe from this resource and stop receiving updates. |
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.
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
and loadedAll
.
- latestSnapshot: MessageSnapshot[] | null
the current state of the messages in the window, or null if you're not a participant in the conversation
- loadedAll: boolean
true when latestSnapshot
contains all the messages in the conversation
When type
is "error", includes the error
field. It is a JS Error
object explaining what caused the subscription to be terminated.
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.
messageSubscription.loadMore(count): Promise<void>
Expand the window to include older messages
Calling loadMore
multiple times in parallel will still only load one page of messages.
Parameters
The number of additional messages to load. Must be between 1 and 100
Returns
A promise that resolves once the additional messages have loaded
messageSubscription.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
A snapshot of the typing indicators in a conversation at a given moment in time. Will be either FewTypingSnapshot when only a few people are typing, or ManyTypingSnapshot when many people are typing.
Currently FewTypingSnapshot is used when there are 5 or less people typing in the conversation, and ManyTypingSnapshot is used when more than 5 people are typing. This limit may change in the future, which will not be considered a breaking change.
1function formatTyping(snapshot: TypingSnapshot): string {2 if (snapshot.many) {3 return "Several people are typing";4 }56 const names = snapshot.users.map(user => user.name);78 if (names.length === 0) {9 return "";10 }1112 if (names.length === 1) {13 return names[0] + " is typing";14 }1516 if (names.length === 2) {17 return names.join(" and ") + " are typing";18 }1920 // Prefix last name with "and "21 names.push("and " + names.pop());22 return names.join(", ") + " are typing";23}
The TypingSnapshot variant used when only a few people are typing.
Check this to differentiate between FewTypingSnapshot (false
) and ManyTypingSnapshot (true
).
When false
, you can see the list of users who are typing in the users
property.
The users who are currently typing in this conversation.
The list is in chronological order, starting with the users who have been typing the longest. The current user is never contained in the list, only other users.
The TypingSnapshot variant used when many people are typing.
Check this to differentiate between FewTypingSnapshot (false
) and ManyTypingSnapshot (true
).
When true
, you do not receive a list of users who are typing. You should show a message like "several people are typing" instead.
A subscription to the typing status in a specific conversation
Get a TypingSubscription by calling ConversationRef.subscribeTyping.
When there are "many" people typing (meaning you received ManyTypingSnapshot), the next update you receive will be FewTypingSnapshot once enough people stop typing. Until then, your ManyTypingSnapshot is still valid and doesn not need to changed, so onSnapshot
will not be called.
unsubscribe | Unsubscribe from this resource and stop receiving updates. |
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.
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: TypingSnapshot | null
. It is the current state of the typing indicators, or null if you're not a participant in the conversation
When type
is "error", includes the error: Error
field. It is a JS Error
object explaining what caused the subscription to be terminated.
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.
typingSubscription.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.