pamoja for TypeScript - v0.2.0
    Preparing search index...

    An MQTT client transport.

    Construct it with broker settings, connect, then publish, subscribe, and read inbound messages with recv or by iterating the client with for await.

    const client = new MqttClient({ clientId: 'sensor-1', host: 'localhost', port: 1883 })
    await client.connect()
    await client.subscribe('sensors/+/temperature')
    await client.publish('sensors/1/temperature', '21.5')
    for await (const message of client) {
    console.log(message.topic, message.payload.toString())
    }
    Index
    • Iterates incoming messages, so a client can be used with for await.

      Returns AsyncGenerator<mqtt.MqttMessage, void, unknown>

    • Connects to the broker and starts the background event loop.

      Returns Promise<void>

      A promise that resolves once connected and rejects on failure.

    • Closes the connection and stops the background event loop.

      Returns Promise<void>

      A promise that resolves once the client has disconnected.

    • Reports whether the client currently holds an active connection.

      Returns Promise<boolean>

      A promise resolving to the connection state.

    • Yields messages from subscribed topics until the connection ends. A connection that ends on its own throws its reason out of the loop.

      Returns AsyncGenerator<mqtt.MqttMessage, void, unknown>

      An async generator over incoming messages.

    • Publishes a payload to a topic.

      Parameters

      • topic: string

        The destination topic.

      • payload: string | Uint8Array<ArrayBufferLike>

        The message body; strings are encoded as UTF-8.

      • Optionaloptions: mqtt.MqttPublishOptions

        The quality of service and retain flag for this message.

      Returns Promise<void>

      A promise that resolves once the payload is queued for the broker, before the broker acknowledges it; publishConfirmed waits for that.

    • Publishes a payload to a topic and waits for the broker to acknowledge it: its PUBACK at AtLeastOnce, its PUBCOMP at ExactlyOnce, and once the connection has taken it at AtMostOnce, where MQTT acknowledges nothing.

      Parameters

      • topic: string

        The destination topic.

      • payload: string | Uint8Array<ArrayBufferLike>

        The message body; strings are encoded as UTF-8.

      • Optionaloptions: mqtt.MqttPublishOptions

        The quality of service and retain flag for this message.

      Returns Promise<void>

      A promise that resolves once the broker holds the message, and rejects if the connection ends first, when the message may or may not have arrived.

    • Awaits the next message from any subscribed topic.

      A connection that ends on its own, because the broker went away or another client connected with the same id, rejects one receive with the reason; after that the receive resolves to null.

      Parameters

      • OptionaltimeoutMs: number

        How long to wait before rejecting. A message that arrives later waits for the next receive. Without it the receive waits as long as it takes.

      Returns Promise<mqtt.MqttMessage | null>

      The next message, or null once the connection has ended.

    • Publishes a payload to a topic at the client's default quality of service.

      This is the send every link has, so a profile's Node or a RuleEngine publishes over an MQTT client the way it publishes over any other link.

      Parameters

      • topic: string

        The destination topic.

      • payload: string | Uint8Array<ArrayBufferLike>

        The message body; strings are encoded as UTF-8.

      Returns Promise<void>

      A promise that resolves once the payload is queued for the broker.

    • Subscribes to a topic filter.

      Parameters

      • topic: string

        The topic or wildcard filter to subscribe to.

      Returns Promise<void>

      A promise that resolves once the subscription is registered.