UNPKG

6.53 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import * as events from 'events'
4import {
5 IClientOptions,
6 IClientPublishOptions,
7 IClientSubscribeOptions,
8 IClientReconnectOptions
9} from './client-options'
10import { Store } from './store'
11import { Packet, QoS } from './types'
12
13export interface ISubscriptionGrant {
14 /**
15 * is a subscribed to topic
16 */
17 topic: string
18 /**
19 * is the granted qos level on it, may return 128 on error
20 */
21 qos: QoS | number
22}
23export interface ISubscriptionRequest {
24 /**
25 * is a subscribed to topic
26 */
27 topic: string
28 /**
29 * is the granted qos level on it
30 */
31 qos: QoS
32}
33export interface ISubscriptionMap {
34 /**
35 * object which has topic names as object keys and as value the QoS, like {'test1': 0, 'test2': 1}.
36 */
37 [topic: string]: QoS
38}
39
40export declare type ClientSubscribeCallback = (err: Error, granted: ISubscriptionGrant[]) => void
41export declare type OnMessageCallback = (topic: string, payload: Buffer, packet: Packet) => void
42export declare type OnPacketCallback = (packet: Packet) => void
43export declare type OnErrorCallback = (error: Error) => void
44export declare type PacketCallback = (error?: Error, packet?: Packet) => any
45export declare type CloseCallback = () => void
46
47export interface IStream extends events.EventEmitter {
48 pipe (to: any): any
49 destroy (): any
50 end (): any
51}
52/**
53 * MqttClient constructor
54 *
55 * @param {Stream} stream - stream
56 * @param {Object} [options] - connection options
57 * (see Connection#connect)
58 */
59export declare class MqttClient extends events.EventEmitter {
60 public connected: boolean
61 public disconnecting: boolean
62 public disconnected: boolean
63 public reconnecting: boolean
64 public incomingStore: Store
65 public outgoingStore: Store
66 public options: IClientOptions
67 public queueQoSZero: boolean
68
69 constructor (streamBuilder: (client: MqttClient) => IStream, options: IClientOptions)
70
71 public on (event: 'message', cb: OnMessageCallback): this
72 public on (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
73 public on (event: 'error', cb: OnErrorCallback): this
74 public on (event: string, cb: Function): this
75
76 public once (event: 'message', cb: OnMessageCallback): this
77 public once (event:
78 'packetsend'
79 | 'packetreceive', cb: OnPacketCallback): this
80 public once (event: 'error', cb: OnErrorCallback): this
81 public once (event: string, cb: Function): this
82
83 /**
84 * publish - publish <message> to <topic>
85 *
86 * @param {String} topic - topic to publish to
87 * @param {(String|Buffer)} message - message to publish
88 *
89 * @param {Object} [opts] - publish options, includes:
90 * @param {Number} [opts.qos] - qos level to publish on
91 * @param {Boolean} [opts.retain] - whether or not to retain the message
92 *
93 * @param {Function} [callback] - function(err){}
94 * called when publish succeeds or fails
95 * @returns {Client} this - for chaining
96 * @api public
97 *
98 * @example client.publish('topic', 'message')
99 * @example
100 * client.publish('topic', 'message', {qos: 1, retain: true})
101 * @example client.publish('topic', 'message', console.log)
102 */
103 public publish (topic: string, message: string | Buffer,
104 opts: IClientPublishOptions, callback?: PacketCallback): this
105 public publish (topic: string, message: string | Buffer,
106 callback?: PacketCallback): this
107
108 /**
109 * subscribe - subscribe to <topic>
110 *
111 * @param {String, Array, Object} topic - topic(s) to subscribe to, supports objects in the form {'topic': qos}
112 * @param {Object} [opts] - optional subscription options, includes:
113 * @param {Number} [opts.qos] - subscribe qos level
114 * @param {Function} [callback] - function(err, granted){} where:
115 * {Error} err - subscription error (none at the moment!)
116 * {Array} granted - array of {topic: 't', qos: 0}
117 * @returns {MqttClient} this - for chaining
118 * @api public
119 * @example client.subscribe('topic')
120 * @example client.subscribe('topic', {qos: 1})
121 * @example client.subscribe({'topic': 0, 'topic2': 1}, console.log)
122 * @example client.subscribe('topic', console.log)
123 */
124 public subscribe (topic:
125 string
126 | string[], opts: IClientSubscribeOptions, callback?: ClientSubscribeCallback): this
127 public subscribe (topic:
128 string
129 | string[]
130 | ISubscriptionMap, callback?: ClientSubscribeCallback): this
131
132 /**
133 * unsubscribe - unsubscribe from topic(s)
134 *
135 * @param {String, Array} topic - topics to unsubscribe from
136 * @param {Function} [callback] - callback fired on unsuback
137 * @returns {MqttClient} this - for chaining
138 * @api public
139 * @example client.unsubscribe('topic')
140 * @example client.unsubscribe('topic', console.log)
141 */
142 public unsubscribe (topic: string | string[], callback?: PacketCallback): this
143
144 /**
145 * end - close connection
146 *
147 * @returns {MqttClient} this - for chaining
148 * @param {Boolean} force - do not wait for all in-flight messages to be acked
149 * @param {Function} cb - called when the client has been closed
150 *
151 * @api public
152 */
153 public end (force?: boolean, cb?: CloseCallback): this
154
155 /**
156 * removeOutgoingMessage - remove a message in outgoing store
157 * the outgoing callback will be called withe Error('Message removed') if the message is removed
158 *
159 * @param {Number} mid - messageId to remove message
160 * @returns {MqttClient} this - for chaining
161 * @api public
162 *
163 * @example client.removeOutgoingMessage(client.getLastMessageId());
164 */
165 public removeOutgoingMessage (mid: number): this
166
167 /**
168 * reconnect - connect again using the same options as connect()
169 *
170 * @param {Object} [opts] - optional reconnect options, includes:
171 * {Store} incomingStore - a store for the incoming packets
172 * {Store} outgoingStore - a store for the outgoing packets
173 * if opts is not given, current stores are used
174 *
175 * @returns {MqttClient} this - for chaining
176 *
177 * @api public
178 */
179 public reconnect (opts?: IClientReconnectOptions): this
180
181 /**
182 * Handle messages with backpressure support, one at a time.
183 * Override at will.
184 *
185 * @param packet packet the packet
186 * @param callback callback call when finished
187 * @api public
188 */
189 public handleMessage (packet: Packet, callback: PacketCallback): void
190
191 /**
192 * getLastMessageId
193 */
194 public getLastMessageId (): number
195}
196export { IClientOptions }