Room reactions
Users can send reactions to the entire chat room to show their sentiment as to what is happening. For example, agreeing with the content in a livestream using a thumbs up, or sending a heart when their team scores in a sports game.
Room reactions are ephemeral and not stored or aggregated by Ably. The intention being that they show the overall sentiment of a room at a point in time.
Subscribe to room reactions
Subscribe to room reactions by registering a listener. Use the reactions.subscribe()
method in a room to receive reactions:
val subscription = room.reactions.subscribe { reaction: Reaction ->
println("received a ${reaction.type} with metadata ${reaction.metadata}")
}
CopyCopied!
The following is the structure of a room reaction event:
{
"type": "like",
"headers": {},
"metadata": {
"fireworks": "blue",
},
"clientId": "R3hegPCqgV3DZoMA2sCT-",
"createdAt": "2024-06-12T11:37:59.988Z",
"isSelf": true
}
CopyCopied!
The following are the properties of a room reaction:
Property | Description | Type |
---|---|---|
type | The type reaction of reaction, for example a ‘like’ or a heart emoji. | String |
headers | Optional headers for adding additional information to a reaction. | Object |
metadata | Optional metadata about the reaction, such as an animation or effect. This information is not read by Ably. | Object |
createdAt | The time the reaction was sent. | Date |
clientId | The client identifier of the user that sent the reaction. | String |
isSelf | Will be true for the user that sent the reaction. | Boolean |
Use the unsubscribe()
method on the returned subscription to remove a room reaction listener:
subscription.unsubscribe()
CopyCopied!
Send a room reaction
Use the reactions.send()
method to send a room-level reaction. The most common way of using this method is to trigger it whenever a user clicks an emoji button in a room:
room.reactions.send(type = "like")
room.reactions.send(type = "heart", metadata = JsonObject().apply {
addProperty("effect", "fireworks")
})
CopyCopied!