@seriousme/opifex
Version:
MQTT client & server for Deno & NodeJS
94 lines • 3.57 kB
TypeScript
import { type ConnectPacket, type PublishPacket, type SockConn, type SubscribePacket, type TAuthenticationResult } from "./deps.ts";
type ConnectOptions = Omit<ConnectPacket, "type" | "protocolName" | "protocolLevel">;
/** ConnectParameters define how to connect */
export type ConnectParameters = {
url?: URL;
caCerts?: string[];
cert?: string;
key?: string;
numberOfRetries?: number;
options?: ConnectOptions;
};
/** PublishParameters define how a message should be published */
export type PublishParameters = Omit<PublishPacket, "type" | "id">;
/** SubscribeParameters define how to subscribe to a topic */
export type SubscribeParameters = Omit<SubscribePacket, "type" | "id">;
/** the default MQTT URL to connect to */
export declare const DEFAULT_URL = "mqtt://localhost:1883/";
/**
* The Client class provides an MQTT Client that can be used to connect to
* a MQTT broker and publish/subscribe messages.
*
* The Client class is not meant to be used directly, but
* instead should be subclassed and the subclass should
* override the createConn() method to provide a
* connection type that is supported by the subclass.
*/
export declare class Client {
protected clientIdPrefix: string;
protected numberOfRetries: number;
protected url: URL;
protected keepAlive: number;
protected autoReconnect: boolean;
private caCerts?;
private cert?;
private key?;
private clientId;
private ctx;
private connectPacket?;
/**
* Creates a new MQTT client instance
*/
constructor();
/**
* Creates a new connection to the MQTT broker
* @param protocol - The protocol to use (mqtt, mqtts, etc)
* @param _hostname - The hostname to connect to
* @param _port - The port to connect to
* @param _caCerts - Optional CA certificates
* @param _cert - Optional client certificate
* @param _key - Optional client key
* @returns Promise resolving to a SockConn connection
*/
protected createConn(protocol: string, _hostname: string, _port?: number, _caCerts?: string[], _cert?: string, _key?: string): Promise<SockConn>;
/**
* Handles the connection process including retries and reconnection
* @returns Promise that resolves when connection is established or fails
*/
private doConnect;
/**
* Connects to the MQTT broker
* @param params - Connection parameters
* @returns Promise resolving to authentication result
*/
connect(params?: ConnectParameters): Promise<TAuthenticationResult>;
/**
* Disconnects from the MQTT broker
* @returns Promise that resolves when disconnected
*/
disconnect(): Promise<void>;
/**
* Publishes a message to the MQTT broker
* @param params - Publish parameters including topic and payload
* @returns Promise that resolves when published
*/
publish(params: PublishParameters): Promise<void>;
/**
* Subscribes to topics on the MQTT broker
* @param params - Subscribe parameters including topics
* @returns Promise that resolves when subscribed
*/
subscribe(params: SubscribeParameters): Promise<void>;
/**
* Gets an async iterator for received messages
* @returns AsyncGenerator yielding received publish packets
*/
messages(): AsyncGenerator<PublishPacket, void, unknown>;
/**
* Closes the message stream
* @param reason - Optional reason for closing
*/
closeMessages(reason?: string): void;
}
export {};
//# sourceMappingURL=client.d.ts.map