hammerspoon2-docs
    Preparing search index...

    Class HSMQTTClient

    A connection to an MQTT broker, created by hs.mqtt.create(). The client does nothing until you call connect(). Messages for the topics you subscribe() to arrive as "message" events (see on()). A connected client keeps working after you drop your reference to it, until you call disconnect() or destroy(), or Hammerspoon reloads its configuration. So does a client with listeners registered by on().

    Index

    Constructors

    Properties

    clientID: string

    The MQTT client identifier sent to the broker. Unless you chose one with the clientID option, it is "hammerspoon-" followed by eight random characters.

    host: string

    The host name of the broker.

    identifier: string

    The unique identifier of this client object.

    isConnected: boolean

    Whether the client is connected to the broker.

    port: number

    The port of the broker.

    state: string

    The client's connection state: "disconnected", "connecting", "connected", "reconnecting" (the connection was lost and the client is trying to restore it, see the reconnectInterval option) or "disconnecting".

    subscriptions: Record<string, number>

    The topic filters this client has subscribed to, and the QoS level requested for each. These are restored automatically when the client reconnects.

    Methods

    • Connects to the broker.

      Returns Promise<boolean>

      A Promise resolving once the broker has accepted the connection, to true if the broker resumed a previous session for this client (see the cleanSession option), or false if it started a new one

    • Disconnects from the broker, removes all listeners and releases the client. It can't be used again afterwards. Called automatically when Hammerspoon reloads its configuration.

      Returns void

    • Disconnects from the broker. This also stops a client that is reconnecting from trying again.

      Returns Promise<void>

      A Promise resolving once the client has disconnected

    • Remove a previously registered listener for this client's events.

      Parameters

      • event: "connected" | "disconnected" | "message"

        The event the listener was registered for

      • listener: (...args: any[]) => any

        The function originally passed to on

      Returns void

    • Register a listener for this client's events. A client with listeners stays alive until you remove them or call destroy().

      Parameters

      • event: "connected" | "disconnected" | "message"

        The event to listen for

      • listener: (
            arg?:
                | string
                | {
                    bytes: Uint8Array;
                    duplicate: boolean;
                    payload: string;
                    qos: number;
                    retain: boolean;
                    topic: string;
                }
                | null,
        ) => void

        Called when the event occurs

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • Register a listener that fires at most once for one of this client's events.

      Parameters

      • event: "connected" | "disconnected" | "message"

        The event to listen for

      • listener: (
            arg?:
                | string
                | {
                    bytes: Uint8Array;
                    duplicate: boolean;
                    payload: string;
                    qos: number;
                    retain: boolean;
                    topic: string;
                }
                | null,
        ) => void

        Called once, then automatically removed

      Returns void

      Throws an Error on failure; wrap calls in try/catch to handle it.

    • Publishes a message.

      Parameters

      • topic: string

        The topic to publish to

      • payload: string

        The message, sent as UTF-8. Use JSON.stringify() to send an object

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

        The QoS level to send the message with (default 0), and whether the broker should keep it as the topic's retained message (default false)

      Returns Promise<void>

      A Promise resolving once the message has been sent (QoS 0) or the broker has acknowledged it (QoS 1 and 2)

    • Subscribes to a topic filter, so messages published to matching topics arrive as "message" events.

      Parameters

      • topicFilter: string

        The topic, which may contain the wildcards + (one level) and # (all remaining levels)

      • Optionalqos: 0 | 2 | 1

        The highest QoS level to receive messages with. Defaults to 0

      Returns Promise<number>

      A Promise resolving to the QoS level the broker granted, which may be lower than requested. It rejects if the broker refuses the subscription

    • Unsubscribes from a topic filter.

      Parameters

      • topicFilter: string

        The topic filter originally passed to subscribe()

      Returns Promise<void>

      A Promise resolving once the broker has acknowledged the request