edgeport - v1.0.3
    Preparing search index...

    Interface MqttSession

    A live MQTT session over a single transport.

    Obtain one from connect or connectWebSocket. A background pump reads packets and routes them, so publishing, subscribing, and the QoS acknowledgement handshakes can all be in flight at once. It is an AsyncDisposable, so await using closes it cleanly.

    1.0.0

    interface MqttSession {
        "[asyncDispose]"(): PromiseLike<void>;
        close(opts?: { graceful?: boolean }): Promise<void>;
        publish(
            topic: string,
            payload: string | Uint8Array<ArrayBufferLike>,
            opts?: { qos?: 0 | 1 | 2; retain?: boolean },
        ): Promise<void>;
        publishJson(
            topic: string,
            value: unknown,
            opts?: { qos?: 0 | 1 | 2; retain?: boolean },
        ): Promise<void>;
        subscribe(
            topicFilter: string,
            opts?: { qos?: 0 | 1 | 2 },
        ): MqttSubscription;
        subscribeJson<T = unknown>(
            filter: string,
            opts?: { qos?: 0 | 1 | 2 },
        ): AsyncIterable<MqttJsonMessage<T>, any, any> & AsyncDisposable;
        unsubscribe(topicFilter: string): Promise<void>;
    }

    Hierarchy

    • AsyncDisposable
      • MqttSession
    Index
    • Returns PromiseLike<void>

    • Closes the connection. By default sends a clean DISCONNECT; pass { graceful: false } to drop the socket without one, which makes the broker publish the Last Will (used to simulate an unexpected device disconnect).

      Parameters

      • Optionalopts: { graceful?: boolean }

        graceful (default true): whether to send DISCONNECT first.

      Returns Promise<void>

      Resolves once the transport is closed.

    • Publishes a message to a topic.

      For QoS 0 this resolves once the PUBLISH is written. For QoS 1 it resolves on the PUBACK, and for QoS 2 on the PUBCOMP, so the returned promise tracks broker confirmation.

      Parameters

      • topic: string

        The topic to publish to.

      • payload: string | Uint8Array<ArrayBufferLike>

        The message body (a string is UTF-8 encoded).

      • Optionalopts: { qos?: 0 | 1 | 2; retain?: boolean }

        Optional QoS (default 0) and retain flag.

      Returns Promise<void>

      Resolves once the publish is confirmed at the requested QoS.

      If the session is closed.

    • Publishes a value as JSON to a topic.

      The value is JSON.stringify-ed and encoded as UTF-8, then published with publish. The returned promise tracks broker confirmation at the requested QoS, exactly like publish.

      Parameters

      • topic: string

        The topic to publish to.

      • value: unknown

        The value to serialize and publish.

      • Optionalopts: { qos?: 0 | 1 | 2; retain?: boolean }

        Optional QoS (default 0) and retain flag.

      Returns Promise<void>

      Resolves once the publish is confirmed at the requested QoS.

      If the session is closed.

      1.0.2

    • Subscribes to a topic filter.

      Sends a SUBSCRIBE and returns a subscription whose async iterator yields matching messages. The SUBACK is awaited lazily; the first iteration (or MqttSubscription.unsubscribe) observes any broker rejection.

      Parameters

      • topicFilter: string

        The topic filter (may contain + / # wildcards).

      • Optionalopts: { qos?: 0 | 1 | 2 }

        Optional maximum QoS to request (default 0).

      Returns MqttSubscription

      A subscription async iterable.

    • Subscribes to a topic filter and yields each message with its payload parsed from JSON.

      A thin JSON layer over subscribe: each delivered message's payload is UTF-8 decoded and JSON.parse-d. Iterate the result with for await. It is an AsyncDisposable, so await using unsubscribes automatically.

      Type Parameters

      • T = unknown

        The expected shape of each decoded value.

      Parameters

      • filter: string

        The topic filter (may contain + / # wildcards).

      • Optionalopts: { qos?: 0 | 1 | 2 }

        Optional maximum QoS to request (default 0).

      Returns AsyncIterable<MqttJsonMessage<T>, any, any> & AsyncDisposable

      An async iterable of { topic, value }, also an AsyncDisposable.

      1.0.2

    • Unsubscribes from a topic filter and ends its subscription.

      Parameters

      • topicFilter: string

        The filter previously passed to subscribe.

      Returns Promise<void>

      Resolves once the UNSUBACK is received.