Node.js specific MQTT5 client implementation

Not all parts of the MQTT5 spec are supported. We currently do not support:

  • AUTH packets and the authentication fields in the CONNECT packet
  • QoS 2

MQTT5 Client User Guide

This client is based on native resources. When finished with the client, you must call close() to dispose of them or they will leak.

Hierarchy

Implements

Constructors

  • Client constructor

    Parameters

    Returns Mqtt5Client

Methods

  • Forces all written events to be buffered in memory. The buffered data will be flushed when uncork is called.

    Returns void

  • Synchronously calls each of the listeners registered for the event key supplied in registration order. If the BufferedEventEmitter is currently corked, the event will be buffered until uncork is called.

    Parameters

    • event: EventKey

      The name of the event

    • Rest ...args: any[]

      Event payload

    Returns boolean

  • Registers a listener for the client's error event. An error event is emitted when the client encounters a serious error condition, such as invalid input, napi failures, and other potentially unrecoverable situations.

    Parameters

    • event: "error"

      the type of event to listen to

    • listener: ErrorEventListener

      the event listener to add

    Returns Mqtt5Client

  • Registers a listener for the client's messageReceived event. A messageReceived event is emitted when an MQTT PUBLISH packet is received by the client.

    Parameters

    Returns Mqtt5Client

  • Registers a listener for the client's attemptingConnect event. A attemptingConnect event is emitted every time the client begins a connection attempt.

    Parameters

    Returns Mqtt5Client

  • Registers a listener for the client's connectionSuccess event. A connectionSuccess event is emitted every time the client successfully establishes an MQTT connection.

    Parameters

    Returns Mqtt5Client

  • Registers a listener for the client's connectionFailure event. A connectionFailure event is emitted every time the client fails to establish an MQTT connection.

    Parameters

    Returns Mqtt5Client

  • Registers a listener for the client's disconnection event. A disconnection event is emitted when the client's current MQTT connection is closed for any reason.

    Parameters

    Returns Mqtt5Client

  • Registers a listener for the client's stopped event. A stopped event is emitted when the client finishes shutdown as a result of the user invoking stop.

    Parameters

    • event: "stopped"

      the type of event to listen to

    • listener: StoppedEventListener

      the event listener to add

    Returns Mqtt5Client

  • Send a message to subscribing clients by queuing a PUBLISH packet to be sent to the server.

    Parameters

    Returns Promise<PublishCompletionResult>

    a promise that will be rejected with an error or resolved with the PUBACK response (QoS 1) or undefined (QoS 0)

  • Notifies the MQTT5 client that you want it to maintain connectivity to the configured endpoint. The client will attempt to stay connected using the properties of the reconnect-related parameters in the mqtt5 client configuration.

    This is an asynchronous operation.

    Returns void

  • Notifies the MQTT5 client that you want it to end connectivity to the configured endpoint, disconnecting any existing connection and halting reconnection attempts.

    This is an asynchronous operation. Once the process completes, no further events will be emitted until the client has start invoked. Invoking start() after a stop() will always result in a new MQTT session.

    Parameters

    • Optional disconnectPacket: DisconnectPacket

      (optional) properties of a DISCONNECT packet to send as part of the shutdown process

    Returns void

  • Subscribe to one or more topic filters by queuing a SUBSCRIBE packet to be sent to the server.

    Parameters

    Returns Promise<SubackPacket>

    a promise that will be rejected with an error or resolved with the SUBACK response

  • Flushes all data buffered since cork was called.

    NOTE: It is HIGHLY recommended that uncorking should always be done via process.nextTick, not during the EventEmitter.on() call.

    Returns void

  • Unsubscribe from one or more topic filters by queuing an UNSUBSCRIBE packet to be sent to the server.

    Parameters

    Returns Promise<UnsubackPacket>

    a promise that will be rejected with an error or resolved with the UNSUBACK response

Events

ATTEMPTING_CONNECT: string = 'attemptingConnect'

Event emitted when the client begins a connection attempt.

Listener type: AttemptingConnectEventListener

CONNECTION_FAILURE: string = 'connectionFailure'

Event emitted when the client fails to establish an MQTT connection. Only emitted after an attemptingConnect event.

Listener type: ConnectionFailureEventListener

CONNECTION_SUCCESS: string = 'connectionSuccess'

Event emitted when the client successfully establishes an MQTT connection. Only emitted after an attemptingConnect event.

Listener type: ConnectionSuccessEventListener

DISCONNECTION: string = 'disconnection'

Event emitted when the client's current connection is closed for any reason. Only emitted after a connectionSuccess event.

Listener type: DisconnectionEventListener

ERROR: string = 'error'

Event emitted when the client encounters a serious error condition, such as invalid input, napi failures, and other potentially unrecoverable situations.

Listener type: ErrorEventListener

MESSAGE_RECEIVED: string = 'messageReceived'

Event emitted when an MQTT PUBLISH packet is received by the client.

Listener type: MessageReceivedEventListener

STOPPED: string = 'stopped'

Event emitted when the client finishes shutdown as a result of the user invoking stop.

Listener type: StoppedEventListener

Node-only

  • Triggers cleanup of native resources associated with the MQTT5 client. Once this has been invoked, callbacks and events are not guaranteed to be received.

    This must be called when finished with a client; otherwise, native resources will leak. It is not safe to invoke any further operations on the client after close() has been called.

    For a running client, safe and proper shutdown can be accomplished by

    const stopped = once(client, "stopped");
    client.stop();
    await stopped;
    client.close();

    This is an asynchronous operation.

    Returns void

  • Queries a small set of numerical statistics about the current state of the client's operation queue

    Returns ClientStatistics

  • Queries a small set of numerical statistics about the current state of the client's operation queue

    Returns ClientStatistics

    Deprecated

    use getOperationalStatistics instead

Generated using TypeDoc