aws-crt
Version:
NodeJS/browser bindings to the aws-c-* libraries
376 lines (375 loc) • 14.9 kB
TypeScript
/**
* Browser specific MQTT5 client implementation
*
* DEVELOPER PREVIEW DISCLAIMER
*
* MQTT5 support is currently in **developer preview**. We encourage feedback at all times, but feedback during the
* preview window is especially valuable in shaping the final product. During the preview period we may make
* backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid.
*
* [MQTT5 Client User Guide](https://www.github.com/awslabs/aws-crt-nodejs/blob/main/MQTT5-UserGuide.md)
*
* @packageDocumentation
* @module mqtt5
* @mergeTarget
*
*/
import { BufferedEventEmitter } from "../common/event";
import * as mqtt5 from "../common/mqtt5";
import * as mqtt5_packet from "../common/mqtt5_packet";
import * as auth from "./auth";
export * from "../common/mqtt5";
export * from '../common/mqtt5_packet';
/**
* Factory function that allows the user to completely control the url used to form the websocket handshake
* request.
*/
export declare type Mqtt5WebsocketUrlFactory = () => string;
/**
* Type of url to construct when establishing an MQTT5 connection over websockets
*/
export declare enum Mqtt5WebsocketUrlFactoryType {
/**
* Websocket connection over plain-text with no additional handshake transformation
*/
Ws = 1,
/**
* Websocket connection over TLS with no additional handshake transformation
*/
Wss = 2,
/**
* Websocket connection over TLS with a handshake signed by the Aws Sigv4 signing process
*/
Sigv4 = 3,
/**
* Websocket connection whose url is formed by a user-supplied callback function
*/
Custom = 4
}
/**
* Websocket factory options discriminated union variant for untransformed connections over plain-text
*/
export interface Mqtt5WebsocketUrlFactoryWsOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Ws;
}
/**
* Websocket factory options discriminated union variant for untransformed connections over TLS
*/
export interface Mqtt5WebsocketUrlFactoryWssOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Wss;
}
/**
* Websocket factory options discriminated union variant for untransformed connections over TLS signed by
* the AWS Sigv4 signing process.
*/
export interface Mqtt5WebsocketUrlFactorySigv4Options {
urlFactory: Mqtt5WebsocketUrlFactoryType.Sigv4;
/**
* AWS Region to sign against.
*/
region?: string;
/**
* Provider to source AWS credentials from
*/
credentialsProvider: auth.CredentialsProvider;
}
/**
* Websocket factory options discriminated union variant for arbitrarily transformed handshake urls.
*/
export interface Mqtt5WebsocketUrlFactoryCustomOptions {
urlFactory: Mqtt5WebsocketUrlFactoryType.Custom;
customUrlFactory: Mqtt5WebsocketUrlFactory;
}
/**
* Union of all websocket factory option possibilities.
*/
export declare type Mqtt5WebsocketUrlFactoryOptions = Mqtt5WebsocketUrlFactoryWsOptions | Mqtt5WebsocketUrlFactoryWssOptions | Mqtt5WebsocketUrlFactorySigv4Options | Mqtt5WebsocketUrlFactoryCustomOptions;
/**
* Browser-specific websocket configuration options for connection establishment
*/
export interface Mqtt5WebsocketConfig {
/**
* Options determining how the websocket url is created.
*/
urlFactoryOptions: Mqtt5WebsocketUrlFactoryOptions;
/**
* Opaque options set passed through to the underlying websocket implementation regardless of url factory.
* Use this to control proxy settings amongst other things.
*/
wsOptions?: any;
}
/**
* Configuration options for mqtt5 client creation.
*/
export interface Mqtt5ClientConfig {
/**
* Host name of the MQTT server to connect to.
*/
hostName: string;
/**
* Network port of the MQTT server to connect to.
*/
port: number;
/**
* Controls how the MQTT5 client should behave with respect to MQTT sessions.
*/
sessionBehavior?: mqtt5.ClientSessionBehavior;
/**
* Controls how the reconnect delay is modified in order to smooth out the distribution of reconnection attempt
* timepoints for a large set of reconnecting clients.
*/
retryJitterMode?: mqtt5.RetryJitterType;
/**
* Minimum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed with jitter
* after each connection failure.
*/
minReconnectDelayMs?: number;
/**
* Maximum amount of time to wait to reconnect after a disconnect. Exponential backoff is performed with jitter
* after each connection failure.
*/
maxReconnectDelayMs?: number;
/**
* Amount of time that must elapse with an established connection before the reconnect delay is reset to the minimum.
* This helps alleviate bandwidth-waste in fast reconnect cycles due to permission failures on operations.
*/
minConnectedTimeToResetReconnectDelayMs?: number;
/**
* All configurable options with respect to the CONNECT packet sent by the client, including the will. These
* connect properties will be used for every connection attempt made by the client.
*/
connectProperties?: mqtt5_packet.ConnectPacket;
/**
* Overall time interval to wait to establish an MQTT connection. If a complete MQTT connection (from socket
* establishment all the way up to CONNACK receipt) has not been established before this timeout expires,
* the connection attempt will be considered a failure.
*/
connectTimeoutMs?: number;
/**
* Options for the underlying websocket connection
*
* @group Browser-only
*/
websocketOptions?: Mqtt5WebsocketConfig;
}
/**
* Browser specific MQTT5 client implementation
*
* DEVELOPER PREVIEW DISCLAIMER
*
* MQTT5 support is currently in **developer preview**. We encourage feedback at all times, but feedback during the
* preview window is especially valuable in shaping the final product. During the preview period we may make
* backwards-incompatible changes to the public API, but in general, this is something we will try our best to avoid.
*
* [MQTT5 Client User Guide](https://www.github.com/awslabs/aws-crt-nodejs/blob/main/MQTT5-UserGuide.md)
*/
export declare class Mqtt5Client extends BufferedEventEmitter implements mqtt5.IMqtt5Client {
private config;
private browserClient?;
private state;
private lifecycleEventState;
private lastDisconnect?;
private lastError?;
private reconnectionScheduler?;
private mqttJsConfig;
/**
* Client constructor
*
* @param config The configuration for this client
*/
constructor(config: Mqtt5ClientConfig);
/**
* Triggers cleanup of native resources associated with the MQTT5 client. On the browser, the implementation is
* an empty function.
*/
close(): void;
/**
* 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.
*/
start(): 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 {@link start} invoked. Invoking {@link start start()} after a {@link stop stop()} will always result in
* a new MQTT session.
*
* @param disconnectPacket (optional) properties of a DISCONNECT packet to send as part of the shutdown process
*/
stop(disconnectPacket?: mqtt5_packet.DisconnectPacket): void;
/**
* Subscribe to one or more topic filters by queuing a SUBSCRIBE packet to be sent to the server.
*
* @param packet SUBSCRIBE packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the SUBACK response
*/
subscribe(packet: mqtt5_packet.SubscribePacket): Promise<mqtt5_packet.SubackPacket>;
/**
* Unsubscribe from one or more topic filters by queuing an UNSUBSCRIBE packet to be sent to the server.
*
* @param packet UNSUBSCRIBE packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the UNSUBACK response
*/
unsubscribe(packet: mqtt5_packet.UnsubscribePacket): Promise<mqtt5_packet.UnsubackPacket>;
/**
* Send a message to subscribing clients by queuing a PUBLISH packet to be sent to the server.
*
* @param packet PUBLISH packet to send to the server
* @returns a promise that will be rejected with an error or resolved with the PUBACK response (QoS 1), or
* undefined (QoS 0)
*/
publish(packet: mqtt5_packet.PublishPacket): Promise<mqtt5.PublishCompletionResult>;
/**
* Event emitted when the client encounters a disruptive error condition. Not currently used.
*
* Listener type: {@link ErrorEventListener}
*
* @event
*/
static ERROR: string;
/**
* Event emitted when the client encounters a transient error event that will not disrupt promises based on
* lifecycle events. Currently, mqtt-js client error events are relayed to this event.
*
* Listener type: {@link ErrorEventListener}
*
* @event
* @group Browser-only
*/
static INFO: string;
/**
* Event emitted when an MQTT PUBLISH packet is received by the client.
*
* Listener type: {@link MessageReceivedEventListener}
*
* @event
*/
static MESSAGE_RECEIVED: string;
/**
* Event emitted when the client begins a connection attempt.
*
* Listener type: {@link AttemptingConnectEventListener}
*
* @event
*/
static ATTEMPTING_CONNECT: string;
/**
* Event emitted when the client successfully establishes an MQTT connection. Only emitted after
* an {@link ATTEMPTING_CONNECT attemptingConnect} event.
*
* Listener type: {@link ConnectionSuccessEventListener}
*
* @event
*/
static CONNECTION_SUCCESS: string;
/**
* Event emitted when the client fails to establish an MQTT connection. Only emitted after
* an {@link ATTEMPTING_CONNECT attemptingConnect} event.
*
* Listener type: {@link ConnectionFailureEventListener}
*
* @event
*/
static CONNECTION_FAILURE: string;
/**
* Event emitted when the client's current connection is closed for any reason. Only emitted after
* a {@link CONNECTION_SUCCESS connectionSuccess} event.
*
* Listener type: {@link DisconnectionEventListener}
*
* @event
*/
static DISCONNECTION: string;
/**
* Event emitted when the client finishes shutdown as a result of the user invoking {@link stop}.
*
* Listener type: {@link StoppedEventListener}
*
* @event
*/
static STOPPED: string;
/**
* Registers a listener for the client's {@link ERROR error} event. An {@link ERROR error} event is emitted when
* the client encounters a disruptive error condition.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'error', listener: mqtt5.ErrorEventListener): this;
/**
* Registers a listener for the client's {@link INFO info} event. An {@link INFO info} event is emitted when
* the client encounters a transient error event that will not disrupt promises based on lifecycle events.
* Currently, mqtt-js client error events are relayed to this event.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*
* @group Browser-only
*/
on(event: 'info', listener: mqtt5.ErrorEventListener): this;
/**
* Registers a listener for the client's {@link MESSAGE_RECEIVED messageReceived} event. A
* {@link MESSAGE_RECEIVED messageReceived} event is emitted when an MQTT PUBLISH packet is received by the
* client.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'messageReceived', listener: mqtt5.MessageReceivedEventListener): this;
/**
* Registers a listener for the client's {@link ATTEMPTING_CONNECT attemptingConnect} event. A
* {@link ATTEMPTING_CONNECT attemptingConnect} event is emitted every time the client begins a connection attempt.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'attemptingConnect', listener: mqtt5.AttemptingConnectEventListener): this;
/**
* Registers a listener for the client's {@link CONNECTION_SUCCESS connectionSuccess} event. A
* {@link CONNECTION_SUCCESS connectionSuccess} event is emitted every time the client successfully establishes
* an MQTT connection.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'connectionSuccess', listener: mqtt5.ConnectionSuccessEventListener): this;
/**
* Registers a listener for the client's {@link CONNECTION_FAILURE connectionFailure} event. A
* {@link CONNECTION_FAILURE connectionFailure} event is emitted every time the client fails to establish an
* MQTT connection.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'connectionFailure', listener: mqtt5.ConnectionFailureEventListener): this;
/**
* Registers a listener for the client's {@link DISCONNECTION disconnection} event. A
* {@link DISCONNECTION disconnection} event is emitted when the client's current MQTT connection is closed
* for any reason.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'disconnection', listener: mqtt5.DisconnectionEventListener): this;
/**
* Registers a listener for the client's {@link STOPPED stopped} event. A
* {@link STOPPED stopped} event is emitted when the client finishes shutdown as a
* result of the user invoking {@link stop}.
*
* @param event the type of event to listen to
* @param listener the event listener to add
*/
on(event: 'stopped', listener: mqtt5.StoppedEventListener): this;
private on_browser_disconnect_packet;
private on_browser_close;
private on_browser_client_error;
private on_attempting_connect;
private on_connection_success;
private _on_stopped_internal;
private on_message;
}