mqtt311 - Reference
Constructor
mqtt311 ( {params} )
The constructor of MQTT client requires a table with the following fields:
socket
: an object supplying the methodsclose
,read
&write
(a subset of the cqueues.socket API)logger
: an optional object supplying the methodsdebug
&error
(a subset of the LuaLogging API)on_connect
: an optional callback functionon_message
: an optional callback functionon_error
: an optional callback functionmax_packet_size
: an optional integer
Instance methods
connect ( [{options}] )
Send a CONNECT packet, or throws an error when invalid parameter.
The optional table options
may contains the following fields:
clean
: a boolean Clean Sessionkeep_alive
: an integer Keep Alive, the default value is 0id
: a string Client Identifier, could be auto-generated when Clean Sessionwill
: a table (see below)username
: a string User Namepassword
: a string Password
The array part of the table will
contains the Will Topic
and the Will Message,
the hash part may contains the following fields:
qos
: an value in the set 0, 1 and 2, the default value is 0retain
: a boolean
reconnect (socket)
Replays the call to connect
with a new socket
.
This method is typically called by the callback on_error
.
publish ( topic, payload [, {options}] )
Send a PUBLISH packet with a Topic Name, a Payload, or throws an error when invalid parameter.
The optional table options
may contains the following fields:
qos
: an value in the set 0, 1 and 2, the default value is 0retain
: a boolean
subscribe ( { filter1, qos1, ... } )
Send a SUBSCRIBE packet with a non empty set of pair of Topic Filters and Requested QoS, or throws an error when invalid parameter.
The Requested QoS must be in the set 0, 1 and 2.
unsubscribe ( { filter1, ... } )
Send a UNSUBSCRIBE packet with a non empty list of Topic Filters, or throws an error when invalid parameter.
ping ()
Send a PINGREQ packet.
disconnect ()
Send a DISCONNECT packet.
read ()
Waits and handles a packet from the server.
Returns a table describing this packet or nil
and a message describing the reason of a disconnection
or an unsuccessful reconnection.
The table contains at least the field type
with a string value.
The callback on_connect (return_code, session_present)
is called on CONNACK packet received.
If the connection is successful, this callback does typically some subscriptions.
This callback is not intended to return something.
The callback on_message (topic, payload)
is called on each PUBLISH packet received.
This callback is not intended to return something.
The callback on_error ()
is called when the connection is broken.
When everything is going well, this callback open a new connection,
calls the method reconnect
with it and finally return a true value.
In bad case, this callback returns a false value, optionally followed by a message.
Class functions & members
match (name, filter)
Checks if a Topic Name matches a Topic Filter. Returns a boolean or throws an error when invalid parameter.
PORT
Gives the IANA registered port 1883 as a string.
PORT_TLS
Gives the IANA registered port 8883 as a string.