73fdd9a09c596a4a517a7492dbb70da1f85368f8af476ef81178af5a1ed747b09a4c2f55d725af150d9331ee23a4b5f3f61b363491f821025981437ad2372e 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /// <reference types="node" />
  2. import * as events from 'events'
  3. import {
  4. IClientOptions,
  5. IClientPublishOptions,
  6. IClientSubscribeOptions,
  7. IClientReconnectOptions
  8. } from './client-options'
  9. import { Store } from './store'
  10. import { IAuthPacket, IConnectPacket, IPublishPacket, IDisconnectPacket, IConnackPacket, Packet, QoS } from 'mqtt-packet'
  11. export interface ISubscriptionGrant {
  12. /**
  13. * is a subscribed to topic
  14. */
  15. topic: string
  16. /**
  17. * is the granted qos level on it, may return 128 on error
  18. */
  19. qos: QoS | number
  20. /*
  21. * no local flag
  22. * */
  23. nl?: boolean,
  24. /*
  25. * Retain As Published flag
  26. * */
  27. rap?: boolean,
  28. /*
  29. * Retain Handling option
  30. * */
  31. rh?: number
  32. }
  33. export interface ISubscriptionRequest {
  34. /**
  35. * is a subscribed to topic
  36. */
  37. topic: string
  38. /**
  39. * is the granted qos level on it
  40. */
  41. qos: QoS
  42. /*
  43. * no local flag
  44. * */
  45. nl?: boolean,
  46. /*
  47. * Retain As Published flag
  48. * */
  49. rap?: boolean,
  50. /*
  51. * Retain Handling option
  52. * */
  53. rh?: number
  54. }
  55. export interface ISubscriptionMap {
  56. /**
  57. * object which has topic names as object keys and as value the options, like {'test1': {qos: 0}, 'test2': {qos: 2}}.
  58. */
  59. [topic: string]: {
  60. qos: QoS,
  61. nl?: boolean,
  62. rap?: boolean,
  63. rh?: number
  64. }
  65. }
  66. export declare type OnConnectCallback = (packet: IConnackPacket) => void
  67. export declare type OnDisconnectCallback = (packet: IDisconnectPacket) => void
  68. export declare type ClientSubscribeCallback = (err: Error, granted: ISubscriptionGrant[]) => void
  69. export declare type OnMessageCallback = (topic: string, payload: Buffer, packet: IPublishPacket) => void
  70. export declare type OnPacketCallback = (packet: Packet) => void
  71. export declare type OnCloseCallback = () => void
  72. export declare type OnErrorCallback = (error: Error) => void
  73. export declare type PacketCallback = (error?: Error, packet?: Packet) => any
  74. export declare type CloseCallback = (error?: Error) => void
  75. export interface IStream extends events.EventEmitter {
  76. pipe (to: any): any
  77. destroy (): any
  78. end (): any
  79. }
  80. /**
  81. * MqttClient constructor
  82. *
  83. * @param {Stream} stream - stream
  84. * @param {Object} [options] - connection options
  85. * (see Connection#connect)
  86. */
  87. export declare class MqttClient extends events.EventEmitter {
  88. public connected: boolean
  89. public disconnecting: boolean
  90. public disconnected: boolean
  91. public reconnecting: boolean
  92. public incomingStore: Store
  93. public outgoingStore: Store
  94. public options: IClientOptions
  95. public queueQoSZero: boolean
  96. constructor (streamBuilder: (client: MqttClient) => IStream, options: IClientOptions)
  97. public on (event: 'connect', cb: OnConnectCallback): this
  98. public on (event: 'message', cb: OnMessageCallback): this
  99. public on (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
  100. public on (event: 'disconnect', cb: OnDisconnectCallback): this
  101. public on (event: 'error', cb: OnErrorCallback): this
  102. public on (event: 'close', cb: OnCloseCallback): this
  103. public on (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
  104. public on (event: string, cb: Function): this
  105. public once (event: 'connect', cb: OnConnectCallback): this
  106. public once (event: 'message', cb: OnMessageCallback): this
  107. public once (event: 'packetsend' | 'packetreceive', cb: OnPacketCallback): this
  108. public once (event: 'disconnect', cb: OnDisconnectCallback): this
  109. public once (event: 'error', cb: OnErrorCallback): this
  110. public once (event: 'close', cb: OnCloseCallback): this
  111. public once (event: 'end' | 'reconnect' | 'offline' | 'outgoingEmpty', cb: () => void): this
  112. public once (event: string, cb: Function): this
  113. /**
  114. * publish - publish <message> to <topic>
  115. *
  116. * @param {String} topic - topic to publish to
  117. * @param {(String|Buffer)} message - message to publish
  118. *
  119. * @param {Object} [opts] - publish options, includes:
  120. * @param {Number} [opts.qos] - qos level to publish on
  121. * @param {Boolean} [opts.retain] - whether or not to retain the message
  122. * @param {Function}[opts.cbStorePut] - function(){}
  123. * called when message is put into `outgoingStore`
  124. *
  125. * @param {Function} [callback] - function(err){}
  126. * called when publish succeeds or fails
  127. *
  128. * @returns {Client} this - for chaining
  129. * @api public
  130. *
  131. * @example client.publish('topic', 'message')
  132. * @example
  133. * client.publish('topic', 'message', {qos: 1, retain: true})
  134. * @example client.publish('topic', 'message', console.log)
  135. */
  136. public publish (topic: string, message: string | Buffer,
  137. opts: IClientPublishOptions, callback?: PacketCallback): this
  138. public publish (topic: string, message: string | Buffer,
  139. callback?: PacketCallback): this
  140. /**
  141. * subscribe - subscribe to <topic>
  142. *
  143. * @param {String, Array, Object} topic - topic(s) to subscribe to, supports objects in the form {'topic': qos}
  144. * @param {Object} [opts] - optional subscription options, includes:
  145. * @param {Number} [opts.qos] - subscribe qos level
  146. * @param {Function} [callback] - function(err, granted){} where:
  147. * {Error} err - subscription error (none at the moment!)
  148. * {Array} granted - array of {topic: 't', qos: 0}
  149. * @returns {MqttClient} this - for chaining
  150. * @api public
  151. * @example client.subscribe('topic')
  152. * @example client.subscribe('topic', {qos: 1})
  153. * @example client.subscribe({'topic': 0, 'topic2': 1}, console.log)
  154. * @example client.subscribe('topic', console.log)
  155. */
  156. public subscribe (topic:
  157. string
  158. | string[], opts: IClientSubscribeOptions, callback?: ClientSubscribeCallback): this
  159. public subscribe (topic:
  160. string
  161. | string[]
  162. | ISubscriptionMap, callback?: ClientSubscribeCallback): this
  163. /**
  164. * unsubscribe - unsubscribe from topic(s)
  165. *
  166. * @param {String, Array} topic - topics to unsubscribe from
  167. * @param {Object} opts - opts of unsubscribe
  168. * @param {Function} [callback] - callback fired on unsuback
  169. * @returns {MqttClient} this - for chaining
  170. * @api public
  171. * @example client.unsubscribe('topic')
  172. * @example client.unsubscribe('topic', console.log)
  173. * @example client.unsubscribe('topic', opts, console.log)
  174. */
  175. public unsubscribe (topic: string | string[], opts?: Object, callback?: PacketCallback): this
  176. /**
  177. * end - close connection
  178. *
  179. * @returns {MqttClient} this - for chaining
  180. * @param {Boolean} force - do not wait for all in-flight messages to be acked
  181. * @param {Object} opts - opts disconnect
  182. * @param {Function} cb - called when the client has been closed
  183. *
  184. * @api public
  185. */
  186. public end (force?: boolean, opts?: Object, cb?: CloseCallback): this
  187. /**
  188. * removeOutgoingMessage - remove a message in outgoing store
  189. * the outgoing callback will be called withe Error('Message removed') if the message is removed
  190. *
  191. * @param {Number} mid - messageId to remove message
  192. * @returns {MqttClient} this - for chaining
  193. * @api public
  194. *
  195. * @example client.removeOutgoingMessage(client.getLastMessageId());
  196. */
  197. public removeOutgoingMessage (mid: number): this
  198. /**
  199. * reconnect - connect again using the same options as connect()
  200. *
  201. * @param {Object} [opts] - optional reconnect options, includes:
  202. * {Store} incomingStore - a store for the incoming packets
  203. * {Store} outgoingStore - a store for the outgoing packets
  204. * if opts is not given, current stores are used
  205. *
  206. * @returns {MqttClient} this - for chaining
  207. *
  208. * @api public
  209. */
  210. public reconnect (opts?: IClientReconnectOptions): this
  211. /**
  212. * Handle messages with backpressure support, one at a time.
  213. * Override at will.
  214. *
  215. * @param packet packet the packet
  216. * @param callback callback call when finished
  217. * @api public
  218. */
  219. public handleMessage (packet: Packet, callback: PacketCallback): void
  220. /**
  221. * Handle auth packages for MQTT 5 enhanced authentication methods such
  222. * as challenge response authentication.
  223. *
  224. * Challenge-response authentication flow would look something like this:
  225. *
  226. * --> CONNECT | authMethod = "mathChallenge" -->
  227. * <-- AUTH | authMethod = "mathChallenge", authData = "12 + 34" <--
  228. * --> AUTH | authMethod = "mathChallenge", authData = "46" -->
  229. * <-- CONNACK | reasonCode = SUCCESS <--
  230. *
  231. * This form of authentication has several advantages over traditional
  232. * credential-based approaches. For instance authentication without the direct
  233. * exchange of authentication secrets.
  234. *
  235. * @param packet the auth packet to handle
  236. * @param callback call when finished
  237. * @api public
  238. */
  239. public handleAuth (packet: IAuthPacket, callback: PacketCallback): void
  240. /**
  241. * getLastMessageId
  242. */
  243. public getLastMessageId (): number
  244. }
  245. export { IClientOptions }