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.

class MessageRef

Method Overview

Properties

id
: String
conversationId
: String
_realtimeClient
: RealtimeClient

reaction

messageRef.reaction(emoji): ReactionRef

Parameters

emoji
: String

Returns

ReactionRef

get

messageRef.get(): MessageSnapshot?

Returns

MessageSnapshot?

edit (1)

messageRef.edit(params): Unit

Parameters

params
: String
interface EditTextMessageParams

Parameters you can pass when editing a message.

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

custom (optional)
: Map<String, String?>?

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

text (optional)
: String?

The new text to set as the message body

interface EditMessageParams

Parameters you can pass to {@link 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.

custom (optional)
: Map<String, String?>?

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

content (optional)
: List<ContentBlock>?

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 {@link LinkNode}, {@link ActionLinkNode}, or {@link ActionButtonNode}, as they can be used to trick the recipient.

Returns

Unit

edit (2)

messageRef.edit(params): Unit

Parameters

interface EditTextMessageParams

Parameters you can pass when editing a message.

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

custom (optional)
: Map<String, String?>?

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

text (optional)
: String?

The new text to set as the message body

interface EditMessageParams

Parameters you can pass to {@link 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.

custom (optional)
: Map<String, String?>?

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

content (optional)
: List<ContentBlock>?

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 {@link LinkNode}, {@link ActionLinkNode}, or {@link ActionButtonNode}, as they can be used to trick the recipient.

Returns

Unit

edit (3)

messageRef.edit(params): Unit

Parameters

interface EditTextMessageParams

Parameters you can pass when editing a message.

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

custom (optional)
: Map<String, String?>?

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

text (optional)
: String?

The new text to set as the message body

interface EditMessageParams

Parameters you can pass to {@link 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.

custom (optional)
: Map<String, String?>?

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

content (optional)
: List<ContentBlock>?

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 {@link LinkNode}, {@link ActionLinkNode}, or {@link ActionButtonNode}, as they can be used to trick the recipient.

Returns

Unit

deleteFields

messageRef.deleteFields(fields): Unit

Parameters

fields
: String

Returns

Unit

delete

messageRef.delete(): Unit

Returns

Unit

constructor

new MessageRef.constructor(id, conversationId, _realtimeClient)

Parameters

id
: String
conversationId
: String
_realtimeClient
: RealtimeClient

data class 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.

Method Overview

Properties

id
: String

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

type
: MessageType

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.

sender (optional)
: UserSnapshot?

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.

custom
: Map<String, String>

Custom metadata you have set on the message

createdAt
: Long

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

editedAt (optional)
: Long?

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

referencedMessage
: DeepMessageDataSnapshot?

A snapshot of the message that this message is aa 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.

origin
: MessageOrigin

Where this message origiranted from:

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

"rest" = Message sent via the REST API's "send message" endpoint

"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.

content
: List<ContentBlock>

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

reactions
: List<ReactionSnapshot>

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

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

constructor

new MessageSnapshot.constructor(id, type, sender, custom, createdAt, editedAt, referencedMessage, origin, plaintext, content, reactions)

Parameters

id
: String
type
: MessageType
sender (optional)
: UserSnapshot?
custom
: Map<String, String>
createdAt
: Long
editedAt (optional)
: Long?
referencedMessage
: DeepMessageDataSnapshot?
origin
: MessageOrigin
plaintext
: String
content
: List<ContentBlock>
reactions
: List<ReactionSnapshot>

data class MessageSubscription

Method Overview

Properties

state
: SubscriptionState

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.

connected
: Deferred<SubscriptionState>

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.

terminated
: Deferred<SubscriptionState>

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
: () -> Unit

Unsubscribe from this resource and stop receiving updates.

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

loadMore
: suspend (Int?) -> Unit

Expand the window to include older messages

Calling loadMore multiple times in parallel will still only load one page of messages.

error (optional)
: Exception?

constructor

new MessageSubscription.constructor(state, connected, terminated, unsubscribe, loadMore)

Parameters

state
: SubscriptionState
connected
: Deferred<SubscriptionState>
terminated
: Deferred<SubscriptionState>
unsubscribe
: () -> Unit
loadMore
: suspend (Int?) -> Unit

class ReactionRef

Method Overview

Properties

emoji
: String
messageId
: String
conversationId
: String
_realtimeClient
: RealtimeClient

add

reactionRef.add(): Unit

Returns

Unit

remove

reactionRef.remove(): Unit

Returns

Unit

constructor

new ReactionRef.constructor(emoji, messageId, conversationId, _realtimeClient)

Parameters

emoji
: String
messageId
: String
conversationId
: String
_realtimeClient
: RealtimeClient

data class ReactionSnapshot

A summary of a single emoji reaction on a message.

Method Overview

Properties

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 {@link https://en.wikipedia.org/wiki/Variation_Selectors_(Unicode_block) | variation selector 16 codepoint}. This codepoint forces the character to appear as an emoji.

TalkJS normalises all emoji reactions to be "fully qualified" {@link https://unicode.org/Public/emoji/16.0/emoji-test.txt | 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 16const sent = "👍"// React with thumbs up,await message.reaction(emoji).add()// Fetch the reactionconst snapshot = await message.get();const received = snapshot.reactions[0].emoji;// Fails because TalkJS removed the variation selectorassert sent === received;
Example 1

Unicode emoji "👍"

Example 2

Custom emoji ":cat-roomba:"

count
: Int

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.

constructor

new ReactionSnapshot.constructor(emoji, count, currentUserReacted)

Parameters

emoji
: String
count
: Int
currentUserReacted
: Boolean