The useRoomReactions hook enables sending and subscribing to ephemeral room-level reactions. Room reactions are commonly used for live interactions like floating emojis, applause, or other realtime audience feedback. Unlike message reactions, room reactions are not persisted.
1
2
3
4
5
6
7
8
9
10
11
import { useRoomReactions } from '@ably/chat/react';
const MyComponent = () => {
const { sendRoomReaction } = useRoomReactions({
listener: (event) => {
console.log(`${event.reaction.clientId} reacted with ${event.reaction.name}`);
},
});
return <button onClick={() => sendRoomReaction({ name: '👍' })}>React</button>;
};This hook must be used within a ChatRoomProvider.
Parameters
The useRoomReactions hook accepts an optional configuration object:
listeneroptionalRoomReactionListenerReturns
The useRoomReactions hook returns an object with the following properties:
sendRoomReactionsendRoomReaction()roomStatusRoomStatusroomErrorErrorInfo or UndefinedconnectionStatusConnectionStatusconnectionErrorErrorInfo or UndefinedSend a room reaction
sendRoomReaction(params: SendReactionParams): Promise<void>Sends a room-level reaction. Room reactions are ephemeral events that are not associated with specific messages.
Parameters
paramsrequiredSendReactionParamsReturns
Promise<void>
Returns a promise. The promise is fulfilled when the reaction has been sent, or rejected with an ErrorInfo object.
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
29
30
31
32
33
34
35
import { useRoomReactions } from '@ably/chat/react';
import { useState, useCallback } from 'react';
function ReactionBar() {
const [recentReactions, setRecentReactions] = useState([]);
const { sendRoomReaction } = useRoomReactions({
listener: (event) => {
const { reaction } = event;
setRecentReactions((prev) => [
...prev.slice(-9),
{ name: reaction.name, clientId: reaction.clientId, isSelf: reaction.isSelf },
]);
},
});
const emojis = ['👍', '❤️', '😂', '🎉', '👏'];
return (
<div>
<div>
{emojis.map((emoji) => (
<button key={emoji} onClick={() => sendRoomReaction({ name: emoji })}>
{emoji}
</button>
))}
</div>
<div>
{recentReactions.map((r, i) => (
<span key={i}>{r.name}</span>
))}
</div>
</div>
);
}