Messages

A message contains some content that was sent in a conversation. Users send messages using ConversationRef.send. You can track the a conversation's messages in real-time using ConversationRef.subscribeMessages.

interface MessageRef

References the message with a given message ID.

Used in all Data API operations affecting that message, such as fetching or editing the message attributes, or deleting the message. Created via ConversationRef​.message and ConversationRef​.send.

Method Overview

delete

Deletes this message, or does nothing if the message does not exist.

edit

Edits this message.

get

Fetches a snapshot of the message.

reaction

Get a reference to a specific emoji reaction on this message

Properties

conversationId
: string

The ID of the conversation that the referenced message belongs to.

Immutable: if you want to reference a message from a different conversation, get a new MessageRef from that conversation.

id
: string

The ID of the referenced message.

Immutable: if you want to reference a different message, get a new MessageRef instead.

delete

messageRef.delete(): Promise<void>

Deletes this message, or does nothing if the message does not exist.

Deleting a nonexistent message is treated as success, and the promise will resolve.

Returns

Promise<void>

A promise that resolves when the operation completes. This promise will reject if you are not a participant in the conversation or if your role does not give you permission to delete this message.

edit

messageRef.edit(params): Promise<void>

Edits this message.

Parameters

interface EditTextMessageParams

Parameters you can pass to MessageRef​.edit.

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

This is a simpler version of EditMessageParams that only supports setting the message content to text.

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

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

text (optional)
: string

The new text to set in the message body.

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.

interface EditMessageParams

Parameters you can pass to MessageRef​.edit.

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

This is the more advanced method for editing a message. It gives you 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.

content (optional)
: [SendContentBlock]

The new content for the message.

Any value provided here will overwrite the existing message content.

By default users do not have permission to send LinkNode, ActionLinkNode, or ActionButtonNode, as they can be used to trick the recipient.

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

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

Returns

Promise<void>

A promise that resolves when the operation completes. The promise will reject if the request is invalid, the message doesn't exist, or you do not have permission to edit that message.

get

messageRef.get(): Promise<MessageSnapshot | null>

Fetches a snapshot of the message.

Supports Cached Fetch

Returns

Promise<MessageSnapshot | null>

A snapshot of the message's attributes, or null if the message doesn't exist, the conversation doesn't exist, or you're not a participant in the conversation.

reaction

messageRef.reaction(emoji): ReactionRef

Get a reference to a specific emoji reaction on this message

If you call .reaction with an invalid emoji, it will still succeed and you will still get a ReactionRef. However, the TalkJS server will reject any calls that use an invalid emoji.

In the future, this will also be used to fetch a full list of people who used that specific reaction on the message.

Parameters

emoji
: string

The emoji for the reaction you want to reference. a single Unicode emoji like "🚀" or a custom emoji like ":cat_roomba:". Custom emoji can be up to 50 characters long.

Returns

ReactionRef

A for the reaction with that emoji on this message

Usage

Example 1
Reacting to the message with a Unicode emoji
1MessageRef.reaction("🚀").add()
Example 2
Removing your custom emoji reaction from the message
1MessageRef.reaction(":cat-roomba:").remove()

interface MessageSnapshot

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

Automatically expanded to include a snapshot of the user that sent the message, and a snapshot of the referenced message, if this message is a reply.

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

Properties

type
: "UserMessage" | "SystemMessage"

Whether this message was "from a user" or a general system message without a specific sender.

The sender property is always present for "UserMessage" messages and never present for "SystemMessage" messages.

content
: ContentBlock[]

The main body of the message, as a list of blocks that are rendered top-to-bottom.

createdAt
: number

Time at which the message was sent, as a unix timestamp in milliseconds

custom
: Record<string, string>

Custom metadata you have set on the message

editedAt
: number | null

Time at which the message was last edited, as a unix timestamp in milliseconds. null if the message has never been edited.

id
: string

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

origin
: "web" | "rest" | "import" | "email"

Where this message originated from:

- "web" = Message sent via the UI or via ConversationBuilder​.sendMessage

- "rest" = Message sent via the REST API's "send message" endpoint or ConversationRef​.send

- "import" = Message sent via the REST API's "import messages" endpoint

- "email" = Message sent by replying to an email notification

plaintext
: string

The contents of the message, as a plain text string without any formatting or attachments. Useful for showing in a conversation list or in notifications.

reactions
: ReactionSnapshot[]

All the emoji reactions that have been added to this message.

There can be up to 50 different reactions on each message.

referencedMessage
: ReferencedMessageSnapshot | null

A snapshot of the message that this message is a reply to, or null if this message is not a reply.

Only UserMessages can reference other messages. The referenced message snapshot does not have a referencedMessage field. Instead, it has referencedMessageId. This prevents TalkJS fetching an unlimited number of messages in a long chain of replies.

sender
: UserSnapshot | null

A snapshot of the user who sent the message, or null if it is a system message. The user's attributes may have been updated since they sent the message, in which case this snapshot contains the updated data. It is not a historical snapshot.

interface ReferencedMessageSnapshot

A snapshot of a message's attributes at a given moment in time, used in MessageSnapshot​.referencedMessage.

Automatically expanded to include a snapshot of the user that sent the message. Since this is a snapshot of a referenced message, its referenced message is not automatically expanded, to prevent fetching an unlimited number of messages in a long chain of replies. Instead, contains the referencedMessageId field.

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

Properties

type
: "UserMessage"

Referenced messages are always "UserMessage" because you cannot reply to a system message.

content
: ContentBlock[]

The main body of the message, as a list of blocks that are rendered top-to-bottom.

createdAt
: number

Time at which the message was sent, as a unix timestamp in milliseconds

custom
: Record<string, string>

Custom metadata you have set on the message

editedAt
: number | null

Time at which the message was last edited, as a unix timestamp in milliseconds. null if the message has never been edited.

id
: string

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

origin
: "web" | "rest" | "import" | "email"

Where this message originated from:

- "web" = Message sent via the UI or via ConversationBuilder​.sendMessage

- "rest" = Message sent via the REST API's "send message" endpoint or ConversationRef​.send

- "import" = Message sent via the REST API's "import messages" endpoint

- "email" = Message sent by replying to an email notification

plaintext
: string

The contents of the message, as a plain text string without any formatting or attachments. Useful for showing in a conversation list or in notifications.

reactions
: ReactionSnapshot[]

All the emoji reactions that have been added to this message.

referencedMessageId
: string | null

The ID of the message that this message is a reply to, or null if this message is not a reply.

Since this is a snapshot of a referenced message, we do not automatically expand its referenced message. The ID of its referenced message is provided here instead.

sender
: UserSnapshot

A snapshot of the user who sent the message. The user's attributes may have been updated since they sent the message, in which case this snapshot contains the updated data. It is not a historical snapshot.

Guaranteed to be set, unlike in MessageSnapshot, because you cannot reference a SystemMessage

interface MessageSubscription

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 that are sent after you subscribe.

You can expand this window by calling loadMore, which extends the window further into the past.

Method Overview

loadMore

Expand the window to include older messages

unsubscribe

Unsubscribe from this resource and stop receiving updates.

Properties

connected
: Promise<MessageActiveState>

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.

state
: PendingState | MessageActiveState | 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 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.

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.

loadMore

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

count (optional)
: number

The number of additional messages to load. Must be between 1 and 100. Default 30.

Returns

Promise<void>

A promise that resolves once the additional messages have loaded

unsubscribe

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

void

interface ReactionRef

References a specific emoji reaction on a message.

Used in all Data API operations affecting that emoji reaction, such as adding or removing the reaction. Created via MessageRef​.reaction.

Method Overview

add

Adds this emoji reaction onto the message, from the current user.

remove

Removes this emoji reaction from the message, from the current user.

Properties

conversationId
: string

The ID of the conversation the message belongs to.

Immutable: if you want to reference a message from a different conversation, get a new MessageRef from that conversation and call .reaction on that MessageRef.

emoji
: string

Which emoji the reaction is using.

Either a single Unicode emoji, or the name of a custom emoji with a colon at the start and end. This is not validated until you send a request to the server. Since custom emoji are configured in the frontend, there are no checks to make sure a custom emoji actually exists.

Immutable: if you want to use a different emoji, get a new ReactionRef instead.

Example 1
Unicode emoji "👍"
Example 2
Custom emoji ":cat-roomba:"
messageId
: string

The ID of the message that this is a reaction to.

Immutable: if you want to react to a different message, get a new ReactionRef instead.

add

reactionRef.add(): Promise<void>

Adds this emoji reaction onto the message, from the current user.

Returns

Promise<void>

A promise that resolves when the operation completes. The promise will reject if the request is invalid, the message doesn't exist, there are already 50 different reactions on this message, or if you do not have permission to use emoji reactions on that message.

remove

reactionRef.remove(): Promise<void>

Removes this emoji reaction from the message, from the current user.

Returns

Promise<void>

A promise that resolves when the operation completes. The promise will reject if the request is invalid, the message doesn't exist, or you do not have permission to use emoji reactions on that message.

interface ReactionSnapshot

A summary of a single emoji reaction on a message.

Properties

count
: number

The number of times this emoji has been added to the message.

currentUserReacted
: boolean

Whether the current user has reacted to the message with this emoji.

emoji
: string

Which emoji the users reacted with.

Either a single Unicode emoji, or the name of a custom emoji with a colon at the start and end. Since custom emoji are defined in the frontend, they are not validated by the TalkJS server. The UI should ignore reactions that use unrecognised custom emoji.

NOTE: In unicode, it is possible to have multiple emoji that look identical but are represented differently. For example, "👍" !== "👍️" because the second emoji includes a variation selector 16 codepoint. This codepoint forces the character to appear as an emoji.

TalkJS normalises all emoji reactions to be "fully qualified" according to this list. This prevents a message having multiple separate 👍 reactions.

Be careful when processing the emoji property, as this normalisation might break equality checks:

1// Emoji has unnecessary variation selector 16
2const sent = "👍"
3
4// React with thumbs up,
5await message.reaction(emoji).add()
6
7// Fetch the reaction
8const snapshot = await message.get();
9const received = snapshot.reactions[0].emoji;
10
11// Fails because TalkJS removed the variation selector
12assert sent === received;
Example 1
Unicode emoji "👍"
Example 2
Custom emoji ":cat-roomba:"