forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt.d.ts
448 lines (402 loc) · 13.9 KB
/
mqtt.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Type definitions for MQTT
// Project: https://github.com/mqttjs/MQTT.js
// Definitions by: Pekka Leppänen <https://github.com/PekkaPLeppanen>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
declare module 'mqtt' {
namespace mqtt {
import ReadableStream = NodeJS.ReadableStream;
import EventEmitter = NodeJS.EventEmitter;
interface Packet {
messageId: string;
[key: string]: any;
}
interface Granted {
/**
* is a subscribed to topic
*/
topic: string;
/**
* is the granted qos level on it
*/
qos: number;
}
interface Topic {
/**
* object which has topic names as object keys and as value the QoS, like {'test1': 0, 'test2': 1}.
*/
[topic: string]: number;
}
/**
* MQTT CLIENT
*/
interface ClientOptions extends SecureClientOptions {
/**
* 10 seconds, set to 0 to disable
*/
keepalive?: number;
/**
* 'mqttjs_' + Math.random().toString(16).substr(2, 8)
*/
clientId?: string;
/**
* 'MQTT'
*/
protocolId?: string;
/**
* 4
*/
protocolVersion?: number;
/**
* true, set to false to receive QoS 1 and 2 messages while offline
*/
clean?: boolean;
/**
* 1000 milliseconds, interval between two reconnections
*/
reconnectPeriod?: number;
/**
* 30 * 1000 milliseconds, time to wait before a CONNACK is received
*/
connectTimeout?: number;
/**
* the username required by your broker, if any
*/
username?: string;
/**
* the password required by your broker, if any
*/
password?: string;
/**
* a Store for the incoming packets
*/
incomingStore?: Store;
/**
* a Store for the outgoing packets
*/
outgoingStore?: Store;
/**
* a message that will sent by the broker automatically when the client disconnect badly.
*/
will?: {
/**
* the topic to publish
*/
topic: string;
/**
* the message to publish
*/
payload: string;
/**
* the QoS
*/
qos: number;
/**
* the retain flag
*/
retain: boolean;
};
}
interface SecureClientOptions {
/**
* path to private key
*/
keyPath?: string;
/**
* path to corresponding public cert
*/
certPath?: string;
rejectUnauthorized?: boolean;
}
interface ClientPublishOptions {
/**
* the QoS
*/
qos?: number;
/**
* the retain flag
*/
retain?: boolean;
}
interface ClientSubscribeOptions {
/**
* the QoS
*/
qos?: number;
}
interface ClientSubscribeCallback {
(err: any, granted: Granted): void;
}
/**
* @deprecated use connect() instead
* Create a new IClient (see: IClient)
*
* @param port - broker port (default: 1883)
* @param host - broker host (default: localhost)
* @param options - connect options
*/
function createClient(port?: number, host?: string, options?: ClientOptions): Client;
/**
* @deprecated use connect() instead
* Create a new secure IClient
*
* @param port
* @param host
* @param options - connection options, must include keys.
*/
function createSecureClient(port?: number, host?: string, options?: SecureClientOptions): Client;
/**
* Create a new MqttClient (see: IClient)
*
* The brokerUrl supports normal connections using mqtt:// or tcp:// and secure connections using mqtts:// or ssl://.
*
* Passing the clientId is also supported, for example mqtt://user@localhost?clientId=123abc.
*
* @param brokerUrl
* @param options
*/
function connect(brokerUrl: string, options?: ClientOptions): Client;
/**
* The Client class wraps a client connection to an MQTT broker over an arbitrary transport method (TCP, TLS, WebSocket, ecc).
*
* Client automatically handles the following:
* - Regular server pings
* - QoS flow
* - Automatic reconnections
* - Start publishing before being connected
*
*/
interface Client extends EventEmitter {
(streamBuilder: any, options: ClientOptions): Client;
/**
* Publish a message to a topic
*
* @param topic
* @param message
* @param options
* @param callback
*/
publish(topic: string, message: Buffer, options?: ClientPublishOptions, callback?: Function): Client;
publish(topic: string, message: string, options?: ClientPublishOptions, callback?: Function): Client;
/**
* Subscribe to a topic or topics
* @param topic to subscribe to or an Array of topics to subscribe to. It can also be an object.
* @param the options to subscribe with
* @param callback fired on suback
*/
subscribe(topic: string, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client;
subscribe(topic: string[], options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client;
subscribe(topic: Topic, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client;
/**
* Unsubscribe from a topic or topics
*
* @param topic is a String topic or an array of topics to unsubscribe from
* @param options
* @param callback fired on unsuback
*/
unsubscribe(topic: string, options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client;
unsubscribe(topic: string[], options?: ClientSubscribeOptions, callback?: ClientSubscribeCallback): Client;
/**
* end - close connection
*
* @param force passing it to true will close the client right away, without waiting for the in-flight messages to be acked.
* This parameter is optional.
* @param callback
*/
end(force?: boolean, callback?: Function): Client;
/**
* Handle messages with backpressure support, one at a time. Override at will, but always call callback, or the client will
* hang.
*
* @param packet
* @param callback
*/
handleMessage(packet: Packet, callback: Function): Client;
}
/**
* STORE
*/
/**
* In-memory implementation of the message store.
*
* Another implementaion is mqtt-level-store which uses Level-browserify to store the inflight data,
* making it usable both in Node and the Browser.
*/
interface Store {
/**
* Adds a packet to the store, a packet is anything that has a messageId property. The callback is called when the packet has
* been stored.
* @param packet
* @param callback
*/
put(packet: Packet, callback: Function): Store;
/**
* get a packet from the store
*
* @param packet
* @param callback
*/
get(packet: Packet, callback: Function): Store;
/**
* Creates a stream with all the packets in the store.
*/
createStream(): ReadableStream;
/**
* Removes a packet from the store, a packet is anything that has a messageId property. The callback is called when the packet
* has been removed.
* @param packet
* @param callback
*/
del(packet: Packet, callback: Function): Store;
/**
* Closes the Store.
* @param callback
*/
close(callback: Function): void;
}
/**
* CONNECTION
*/
/**
* @deprecated use mqtt-connect instead
* Create a new MqttConnection (see: MqttConnection)
*
* @param port - broker port (default: 1883)
* @param host - broker host (default: localhost)
* @param callback - fired on underlying stream connect
*/
function createConnection(port?: number, host?: string, callback?: Function): Connection;
interface ConnectOptions {
/**
* Protocol ID, usually MQIsdp.
*/
protocolId?: string;
/**
* Protocol version, usually 3.
*/
protocolVersion?: number;
/**
* keepalive period in seconds.
*/
keepalive?: number;
/**
* client ID.
*/
clientId?: string;
/**
* the client's will message options
*/
will?: {
/**
* the topic to publish
*/
topic: string;
/**
* the message to publish
*/
payload: string;
/**
* the QoS
*/
qos: number;
/**
* the retain flag
*/
retain: boolean;
};
/**
* the 'clean start' flag.
*/
clean?: boolean;
/**
* username for protocol v3.1.
*/
username?: string;
/**
* password for protocol v3.1.
*/
password?: string;
}
interface ConnectionPublishOptions {
/**
* the message ID of the packet, required if qos > 0.
*/
messageId?: number;
/**
* the topic to publish
*/
topic?: string;
/**
* the message to publish
*/
payload?: string;
/**
* the QoS
*/
qos?: number;
/**
* the retain flag
*/
retain?: boolean;
}
/**
* The MqttConnection class represents a raw MQTT connection, both on the server and on the client side. For client side
* operations, it is strongly recommended that MqttClient is used, as MqttConnection requires a great deal of additional
* boilerplate such as setting up error handling and ping request/responses.
*
* If such fine grained control is required, MqttConnection can be instantiated using the mqtt.createConnection method.
*
* MqttServerClient is an unaltered subclass of MqttConnection and can be used in exactly the same way.
*
* @link https://github.com/mqttjs/MQTT.js/wiki/connection
*
*/
interface Connection extends EventEmitter {
/**
* Send an MQTT connect packet.
* @param options
*/
connect(options?: ConnectOptions): Connection;
/**
* Send an MQTT connack packet.
* @param options
*/
connack(options?: { returnCode: number; }): Connection;
/**
* Send an MQTT publish packet.
* @param options
*/
publish(options?: ConnectionPublishOptions): Connection;
}
/**
* SERVER
*/
/**
* @deprecated use connect instead
* Create a new MqttServer (see : IServer)
*
* @param listener - callback called on server client event
*/
function createServer(listener?: Function): Server;
/**
* @deprecated use connect instead
* Create a new MqttSecureServer
* @param keyPath - path to private key file
* @param certPath - path to corresponding public cert
* @param listener - callback called on server client event
*/
function createSecureServer(keyPath: string, certPath: string, listener?: Function): Server;
/**
* The primary methods of instantiating mqtt.js server classes are through the mqtt.createServer and mqtt.createSecureServer
* methods. The former returns an instance of MqttServer and and the latter returns an instance of MqttSecureServer.
*
* While it is possible to instantiate these classes through new MqttServer(), it is strongly recommended to use the factory
* methods
*
* @link https://github.com/mqttjs/MQTT.js/wiki/server
*/
interface Server extends EventEmitter {
}
}
export = mqtt;
}