KxSystems / mqtt

MQTT messaging protocol interface
https://code.kx.com/q/interfaces
Apache License 2.0
9 stars 12 forks source link

Addition of graceful method to disconnect #34

Closed cmccarthy1 closed 4 years ago

cmccarthy1 commented 4 years ago

This PR closes #33

Currently the interface does not allow a user to disconnect 'gracefully' from the single client to which it is connected.

This addition allows a process which is connected to an MQTT broker to disconnect accordingly. The following indicates the behaviour of the interface and the 2 functions added

// Is my process currently connected to a broker?
.mqtt.isConnected[]

// Disconnect from a broker to which you are currently connected
/* timeout in milliseconds to allow the handling of in flight messages
.mqtt.disconnect[timeout]

Example usage:

$ q mqtt.q
KDB+ 4.0 2020.04.16 Copyright (C) 1993-2020 Kx Systems

// Check is connected logic
q).mqtt.isConnected[]
0b
q)hst:`$"tcp://localhost:1883"
q).mqtt.conn[hst;`src;()!()]
q).mqtt.isConnected[]
1b

// Highlight current behaviour by attempting to connect again
// results in a disconn callback
q).mqtt.conn[hst;`src;()!()]
q)(`disconn;())

// Gracefully disconnect and reconnect
q).mqtt.disconnect[1000]
"Client successfully destroyed"
q).mqtt.conn[hst;`src;()!()]

// Attempt to disconnect twice
q).mqtt.disconnect[1000]
"Client successfully destroyed"
q).mqtt.disconnect[1000]
'"No client is currently connected"
  [0] .mqtt.disconnect[1000]
      ^
cmccarthy1 commented 4 years ago