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:
serialStringclientIdStringtextStringtimestampLongmetadataMessageMetadataheadersMap<String, String>actionMessageActionversionMessageVersionreactionsMessageReactionSummaryApply an event to a message
Message.with(event: ChatMessageEvent): MessageCreates 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.
1
val updatedMessage = message.with(updateEvent)An overload is also available for reaction summary events:
Message.with(event: MessageReactionSummaryEvent): Message1
val updatedMessage = message.with(reactionSummaryEvent)Parameters
The with() method takes the following parameters:
eventrequiredChatMessageEvent or MessageReactionSummaryEventReturns
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): MessageCreates a copy of the message with specified fields replaced. This is an extension function on the Message interface.
1
val messageCopy = message.copy(text = "New text")Parameters
The copy() method takes the following parameters:
textoptionalStringheadersoptionalMap<String, String>metadataoptionalJsonObjectReturns
Returns a new message instance with the specified fields replaced.
Example
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")
}
}