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

    Interface UsePresenceResponse

    Common status variables for chat features. Most hooks in this library implement this interface.

    interface UsePresenceResponse {
        connectionError?: ErrorInfo;
        connectionStatus: ConnectionStatus;
        enter: (data?: JsonObject) => Promise<void>;
        leave: (data?: JsonObject) => Promise<void>;
        myPresenceState: { error?: ErrorInfo; present: boolean };
        roomError?: ErrorInfo;
        roomStatus: RoomStatus;
        update: (data?: JsonObject) => Promise<void>;
    }

    Hierarchy (View Summary)

    Index

    Properties

    connectionError?: ErrorInfo

    If there's a connection error it will be available here.

    connectionStatus: ConnectionStatus

    Provides the connection status of the Ably connection.

    enter: (data?: JsonObject) => Promise<void>

    A shortcut to the Presence.enter method, which can be used to manually enter presence when autoEnterLeave is false, or to explicitly re-enter presence with new data.

    Enters the current user into the chat room presence set. Emits an 'enter' event to all presence subscribers. Multiple calls will emit additional update events if the user is already present.

    Note: The room must be attached before calling this method.

    This is a stable reference and will not be changed between renders for the same room.

    Important When called, if UsePresenceParams.autoEnterLeave is set to true, the hook will attempt to auto-enter presence automatically when conditions are met.

    Type Declaration

      • (data?: JsonObject): Promise<void>
      • Parameters

        • Optionaldata: JsonObject

          Optional JSON-serializable data to associate with the user's presence

        Returns Promise<void>

        Promise that resolves when the user has successfully entered, or rejects with RoomInInvalidState if the room is not attached

    // Manual control over presence with conditional logic
    const { enter, leave } = usePresence({ autoEnterLeave: false });

    useEffect(() => {
    if (effectCondition) {
    enter({ status: 'active' });
    }

    return () => {
    if (effectCondition) {
    leave();
    }
    };
    }, [effectCondition, enter, leave]);
    leave: (data?: JsonObject) => Promise<void>

    A shortcut to the Presence.leave method.

    Removes the current user from the chat room presence set. Emits a 'leave' event to all subscribers. If the user is not present, this is a no-op.

    Note: The room must be attached before calling this method.

    This is a stable reference and will not be changed between renders for the same room.

    Important When called, this will prevent the hook from automatically re-entering presence, even when autoEnterLeave is true.

    This is useful for manually controlling when presence is left.

    Type Declaration

      • (data?: JsonObject): Promise<void>
      • Parameters

        • Optionaldata: JsonObject

          Optional final presence data to include with the leave event

        Returns Promise<void>

        Promise that resolves when the user has left the presence set, or rejects with RoomInInvalidState if the room is not attached

    // Manual control over presence with conditional logic
    const { enter, leave } = usePresence({ autoEnterLeave: false });

    useEffect(() => {
    if (effectCondition) {
    enter({ status: 'active' });
    }

    return () => {
    if (effectCondition) {
    leave();
    }
    };
    }, [effectCondition, enter, leave]);
    // Enter presence automatically with some initial data
    const { leave, enter } = usePresence({ initialData: { status: 'online' } });

    // Leave presence explicitly, disabling auto re-entry
    await leave();

    // Re-enter presence again, re-enabling auto-entry if selected in the hook
    await enter({ status: 'online again' })
    myPresenceState: { error?: ErrorInfo; present: boolean }

    The current presence state of this client.

    Type Declaration

    • Optionalerror?: ErrorInfo

      Indicates if an error occurred while trying to enter or leave presence.

    • present: boolean

      Indicates if the user is currently present in the room.

    roomError?: ErrorInfo

    If there's an error with the room it will be available here.

    roomStatus: RoomStatus

    Provides the status of the room.

    update: (data?: JsonObject) => Promise<void>

    A shortcut to the Presence.update method.

    Updates the presence data for the current user in the chat room. Emits an 'update' event to all subscribers. If the user is not already present, they will be entered automatically.

    Note:

    • The room must be attached before calling this method.
    • This method uses PUT-like semantics - the entire presence data is replaced with the new value.

    This is a stable reference and will not be changed between renders for the same room.

    Important When called, if UsePresenceParams.autoEnterLeave is set to true, the hook will attempt to auto-enter presence automatically when conditions are met.

    Type Declaration

      • (data?: JsonObject): Promise<void>
      • Parameters

        • Optionaldata: JsonObject

          JSON-serializable data to replace the user's current presence data

        Returns Promise<void>

        Promise that resolves when the presence data has been updated, or rejects with RoomInInvalidState if the room is not attached