import { ClientIdResolver } from './client-id.js'; import { RoomReactionEvent } from './events.js'; import { Logger } from './logger.js'; import { RoomReactionHeaders, RoomReactionMetadata } from './room-reaction.js'; import { Subscription } from './subscription.js'; import * as Ably from '@ably/pubsub-core'; /** * Params for sending a room-level reactions. Only `name` is mandatory. */ export interface SendReactionParams { /** * The name of the reaction, for example an emoji or a short string (e.g., "❤️", "👏", "confetti", "applause"). * * It is the only mandatory parameter to send a room-level reaction. */ name: string; /** * Optional metadata of the reaction. * * The metadata is a map of extra information that can be attached to the * room reaction. It is not used by Ably and is sent as part of the realtime * message payload. Example use cases are custom animations or other effects. * * Do not use metadata for authoritative information. There is no server-side * validation. When reading the metadata treat it like user input. * */ metadata?: RoomReactionMetadata; /** * Optional headers of the room reaction. * * The headers are a flat key-value map and are sent as part of the realtime * message's `extras` inside the `headers` property. They can serve similar * purposes as the metadata but they are read by Ably and can be used for * features such as * [subscription filters](https://faqs.ably.com/subscription-filters). * * Do not use the headers for authoritative information. There is no * server-side validation. When reading the headers treat them like user * input. * */ headers?: RoomReactionHeaders; } /** * The listener function type for room-level reactions. * @param event The reaction event that was received. */ export type RoomReactionListener = (event: RoomReactionEvent) => void; /** * This interface is used to interact with room-level reactions in a chat room: subscribing to reactions and sending them. * * Get an instance via {@link Room.reactions}. */ export interface RoomReactions { /** * Sends a room-level reaction. * * Room reactions are ephemeral events that are not associated with specific messages. * They're commonly used for live interactions like floating emojis, applause, or other * real-time feedback in chat rooms. Unlike message reactions, room reactions are not * persisted and are only visible to users currently connected to the room. * * **Note**: * - The room should be attached to send room reactions. * - It is possible (though unlikely) to receive your own reaction via subscription before this promise resolves. * @param params - The reaction parameters * @returns Promise that resolves when the reaction has been sent, or rejects with: * - {@link ErrorCode.InvalidArgument} if name is not provided * - {@link ErrorCode.Disconnected} if not connected to Ably * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * const room = await chatClient.rooms.get('live-event'); * * // Attach to the room to send room reactions * await room.attach(); * * // Send a simple room reaction * try { * await room.reactions.send({ * name: '❤️' * }); * console.log('Heart reaction sent to room'); * } catch (error) { * console.error('Failed to send reaction:', error); * } * ``` */ send(params: SendReactionParams): Promise; /** * Subscribes to room-level reaction events. * * Receives all room reactions sent by any user in the room. This is useful for * displaying floating reactions, triggering animations, or showing live audience * engagement in real-time. Room reactions are ephemeral and not persisted. * * **Note**: The room should be attached to receive reaction events. * @param listener - Callback invoked when a room reaction is received * @returns Subscription object with an unsubscribe method * @example * ```typescript * import * as Ably from '@ably/pubsub-device'; * import { ChatClient, RoomReactionEvent } from '@ably/chat'; * * const chatClient: ChatClient; // existing ChatClient instance * * const room = await chatClient.rooms.get('webinar-room'); * * // Subscribe to room reactions for live animations * const subscription = room.reactions.subscribe((event: RoomReactionEvent) => { * const { reaction } = event; * * console.log(`${reaction.clientId} sent ${reaction.name}`); * console.log(`Sent at: ${reaction.createdAt.toISOString()}`); * * // Handle different reaction types * switch (reaction.name) { * case '❤️': * // Show floating heart animation * showFloatingHeart(reaction.isSelf ? 'own' : 'other'); * break; * case '👏': * // Show applause indicator * showApplauseAnimation(reaction.clientId); * break; * default: * // Handle generic reactions * showGenericReaction(reaction.name); * } * * // Check if reaction is from current user * if (reaction.isSelf) { * console.log('You sent a reaction:', reaction.name); * } * }); * * // Attach to the room to start receiving events * await room.attach(); * * // Later, unsubscribe when done * subscription.unsubscribe(); * ``` */ subscribe(listener: RoomReactionListener): Subscription; } /** * @inheritDoc */ export declare class DefaultRoomReactions implements RoomReactions { private readonly _channel; private readonly _connection; private readonly _clientIdResolver; private readonly _logger; private readonly _emitter; private readonly _unsubscribeRoomReactionEvents; /** * Constructs a new `DefaultRoomReactions` instance. * @param channel The Realtime channel instance. * @param connection The connection instance. * @param clientIdResolver The client ID resolver. * @param logger An instance of the Logger. */ constructor(channel: Ably.RealtimeChannel, connection: Ably.Connection, clientIdResolver: ClientIdResolver, logger: Logger); /** * @inheritDoc */ send(params: SendReactionParams): Promise; /** * @inheritDoc */ subscribe(listener: RoomReactionListener): Subscription; private _forwarder; }