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. |
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. |
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.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.
To remove "Alice" from the conversation "Cats"1session.conversation("Cats").participant("Alice").delete();
The user "Alice" will still exist after you do this. This deletes the participant, not the user.
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*");
Reply to a message and set custom message data
1conversationRef.send({2 text: "Agreed",3 referencedMessageId: "...",4 custom: { priority: "HIGH" }5});
Send pre-formatted text with TextBlock
1conversationRef.send({2 content: [{3 type: "text",4 children: [{5 type: "bold",6 children: ["Hello"]7 }]8 }]9});
Send a file with SendFileBlock
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});
Send a location with LocationBlock
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
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.