• Subscribes to a custom event on the swarm bus service and executes a callback when the event is received.

    This function sets up a listener for events with a specified topic on the swarm's bus service, invoking the provided callback with the event's payload when triggered. It is wrapped in beginContext for a clean execution environment, logs the operation if enabled, and enforces restrictions on reserved topic names (defined in DISALLOWED_EVENT_SOURCE_LIST). The callback is queued to ensure sequential processing of events. The function supports a wildcard client ID ("*") for listening to all clients or validates a specific client session. It returns an unsubscribe function to stop listening.

    Type Parameters

    • T extends unknown = any

      The type of the payload data, defaulting to any if unspecified.

    Parameters

    • clientId: string

      The unique identifier of the client session, or "*" for all clients.

    • topicName: string

      The name of the event topic to listen for.

    • fn: (data: T) => void

      Callback function executed when the event is received.

    Returns () => void

    If the topicName is a reserved event source (e.g., "agent-bus"), or if the clientId is not "*" and no session exists.

    const unsubscribe = listenEvent("client-123", "custom-topic", (data) => console.log(data));
    // Logs payload when "custom-topic" event is received for "client-123"
    unsubscribe(); // Stops listening