Message

Open in

The Message interface represents a single message in a chat room. Messages are received through the messages.subscribe() method or retrieved via messages.history().

Properties

The Message interface has the following properties:

serialString
The unique identifier of the message.
clientIdString
The client ID of the user who created the message.
textString
The text content of the message.
timestampLong
The timestamp at which the message was created, in milliseconds since epoch.
metadataMessageMetadata
Extra information attached to the message for features like animations or linking to external resources. Always set; empty object if no metadata was provided.
headersMap<String, String>
Additional information in Ably realtime message extras, usable for features like livestream timestamping or message flagging. Always set; empty map if none provided.
actionMessageAction
The action type indicating if the message was created, updated, or deleted.
versionMessageVersion
Information about the current version of this message.
reactionsMessageReactionSummary
The reactions summary for this message.

Apply an event to a message

Message.with(event: ChatMessageEvent): Message

Creates a new message instance with a chat message event applied. This is useful for updating a local message state when receiving update or delete events. Returns the same instance if the event would be a no-op (for example, applying an older version).

This is an extension function on the Message interface.

Kotlin

1

val updatedMessage = message.with(updateEvent)

An overload is also available for reaction summary events:

Message.with(event: MessageReactionSummaryEvent): Message
Kotlin

1

val updatedMessage = message.with(reactionSummaryEvent)

Parameters

The with() method takes the following parameters:

eventrequiredChatMessageEvent or MessageReactionSummaryEvent
The event to apply to the message.

Returns

Message

Returns a new message instance with the event applied, or the same instance if the event would be a no-op.

Copy a message

Message.copy(text: String = this.text, headers: Map<String, String> = this.headers, metadata: JsonObject = this.metadata): Message

Creates a copy of the message with specified fields replaced. This is an extension function on the Message interface.

Kotlin

1

val messageCopy = message.copy(text = "New text")

Parameters

The copy() method takes the following parameters:

textoptionalString
The new text content. Defaults to the current text.
headersoptionalMap<String, String>
The new headers. Defaults to the current headers.
metadataoptionalJsonObject
The new metadata. Defaults to the current metadata.

Returns

Message

Returns a new message instance with the specified fields replaced.

Example

Kotlin

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

import com.ably.chat.MessageAction

// Subscribe to messages and handle different actions
room.messages.subscribe { event ->
    val message = event.message

    println("Serial: ${message.serial}")
    println("From: ${message.clientId}")
    println("Text: ${message.text}")
    println("Sent at: ${message.timestamp}")
    println("Action: ${message.action}")

    // Check message version for updates or deletions
    if (message.action == MessageAction.MessageUpdate) {
        println("Updated by: ${message.version.clientId}")
        println("Update reason: ${message.version.description}")
    }

    if (message.action == MessageAction.MessageDelete) {
        println("Deleted by: ${message.version.clientId}")
        println("Delete reason: ${message.version.description}")
    }

    // Check reaction summary
    for ((name, summary) in message.reactions.distinct) {
        println("$name: ${summary.total} reactions from ${summary.clientIds.size} clients")
    }
}