@ably/chat - v1.1.1
    Preparing search index...

    Interface Typing

    This interface is used to interact with typing in a chat room including subscribing to typing events and fetching the current set of typing clients.

    Get an instance via Room.typing.

    interface Typing {
        get current(): Set<string>;
        keystroke(): Promise<void>;
        stop(): Promise<void>;
        subscribe(listener: TypingListener): Subscription;
    }
    Index

    Accessors

    Methods

    Accessors

    • get current(): Set<string>

      Gets the current set of users who are typing.

      Returns a Set containing the client IDs of all users currently typing in the room. This provides a snapshot of the typing state at the time of the call.

      Returns Set<string>

      Set of client IDs currently typing

      import * as Ably from 'ably';
      import { ChatClient } from '@ably/chat';

      const chatClient: ChatClient; // existing ChatClient instance

      // Get a room with default options
      const room = await chatClient.rooms.get('support-chat');

      // Attach to the room to start receiving events
      await room.attach();

      // Fetch the current cached set of typing users
      const typingUsers = room.typing.current;

      console.log(`${typingUsers.size} users are typing`);

      if (typingUsers.has('agent-001')) {
      console.log('Support agent is typing a response...');
      }

    Methods

    • Sends a typing started event to notify other users that the current user is typing.

      Events are throttled according to the heartbeatThrottleMs room option to prevent excessive network traffic. If called within the throttle interval, the operation becomes a no-op. Multiple rapid calls are serialized to maintain consistency.

      Note:

      • The connection must be in the connected state.
      • Calls to keystroke() and stop() are serialized and resolve in order.
      • The most recent operation always determines the final typing state.
      • The room must be attached to send typing events.

      Returns Promise<void>

      Promise that resolves when the typing event has been sent, or rejects with:

      import * as Ably from 'ably';
      import { ChatClient } from '@ably/chat';

      const chatClient: ChatClient; // existing ChatClient instance

      // Get a room with default options and attach to it
      const room = await chatClient.rooms.get('project-discussion');
      await room.attach();

      try {
      await room.typing.keystroke();
      } catch (error) {
      console.error('Typing indicator error:', error);
      }
    • Sends a typing stopped event to notify other users that the current user has stopped typing.

      If the user is not currently typing, this operation is a no-op. Multiple rapid calls are serialized to maintain consistency, with the most recent operation determining the final state.

      Note:

      • The connection must be in the connected state.
      • Calls to keystroke() and stop() are serialized and resolve in order.
      • The room must be attached to send typing events.

      Returns Promise<void>

      Promise that resolves when the stop event has been sent, or rejects with:

      import * as Ably from 'ably';
      import { ChatClient } from '@ably/chat';

      const chatClient: ChatClient; // existing ChatClient instance

      // Get a room with default options and attach to it
      const room = await chatClient.rooms.get('customer-support');
      await room.attach();

      // Start typing in the room
      try {
      await room.typing.keystroke();
      } catch (error) {
      console.error('Typing indicator error:', error);
      }

      // User sends a message, or deletes their draft, etc.

      // Stop typing in the room
      try {
      await room.typing.stop();
      } catch (error) {
      console.error('Failed to stop typing:', error);
      }
    • Subscribes to typing events from users in the chat room.

      Receives updates whenever a user starts or stops typing, providing real-time feedback about who is currently composing messages. The subscription emits events containing the current set of typing users and details about what changed.

      Note: The room must be attached to receive typing events.

      Parameters

      • listener: TypingListener

        Callback invoked when the typing state changes

      Returns Subscription

      Subscription object with an unsubscribe method

      import * as Ably from 'ably';
      import { ChatClient, TypingSetEvent } from '@ably/chat';

      const chatClient: ChatClient; // existing ChatClient instance

      // Get a room with default options
      const room = await chatClient.rooms.get('team-chat');

      // Subscribe to typing events
      const subscription = room.typing.subscribe((event: TypingSetEvent) => {
      const { currentlyTyping, change } = event;

      // Display who is currently typing
      if (currentlyTyping.size === 0) {
      hideTypingIndicator();
      } else if (currentlyTyping.size === 1) {
      const [typingUser] = Array.from(currentlyTyping);
      showTypingIndicator(`${typingUser} is typing...`);
      } else if (currentlyTyping.size === 2) {
      const users = Array.from(currentlyTyping);
      showTypingIndicator(`${users[0]} and ${users[1]} are typing...`);
      } else {
      showTypingIndicator(`${currentlyTyping.size} people are typing...`);
      }
      });

      // Attach to the room to start receiving events
      await room.attach();

      // Later, unsubscribe when done
      subscription.unsubscribe();