Module jls.net.mqtt

This module provides classes to work with MQTT.

Message Queuing Telemetry Transport

see MQTT Version 3.1.1

Class MqttClient

MqttClient:new () Creates a new MQTT client.
mqttClient:connect ([addr[, port]]) Connects this MQTT client.
mqttClient:close () Closes this MQTT client.
mqttClient:onMessage (topicName, payload) Called when a message has been published on a subscribed topic.
mqttClient:publish (topicName, payload[, options]) Publishes a message payload on a topic.
mqttClient:subscribe (topicName, qos) Subscribes to a topic.
mqttClient:unsubscribe (topicName) Unubscribes from a topic.

Class MqttServer

MqttServer:new () Creates a new MQTT server.
mqttServer:bind (node[, port[, backlog[, callback]]]) Binds this server to the specified address and port number.
mqttServer:close ([callback]) Closes this server.


Class MqttClient

The MqttClient class enables to Publish and Subscribe to Application Messages.

Usage:

local event = require('jls.lang.event')
local mqtt = require('jls.net.mqtt')

local topicName, payload = 'test', 'Hello world!'
local mqttClient = mqtt.MqttClient:new()
mqttClient:connect():next(function()
  return mqttClient:publish(topicName, payload)
end):next(function()
  mqttClient:close()
end)

event:loop()
MqttClient:new ()
Creates a new MQTT client.

Returns:

    a new MQTT client
mqttClient:connect ([addr[, port]])
Connects this MQTT client.

Parameters:

  • addr string the address to connect to, could be an IP address or a host name. (optional)
  • port number the port to connect to, default is 1883. (optional)

Returns:

    a promise that resolves once the client is connected.
mqttClient:close ()
Closes this MQTT client.

Returns:

    a promise that resolves once the client is closed.
mqttClient:onMessage (topicName, payload)
Called when a message has been published on a subscribed topic.

Parameters:

  • topicName string The name of the topic.
  • payload string The message payload.
mqttClient:publish (topicName, payload[, options])
Publishes a message payload on a topic.

Parameters:

  • topicName string The name of the topic.
  • payload string The message payload.
  • options the options.
    • qos number Quality of Service, 0: At most once delivery, 1: At least once delivery, 2: Exactly once delivery (optional)
    • retain boolean Retain flag, will be delivered to future subscribers (optional)

Returns:

    a promise that resolves once the message is sent.
mqttClient:subscribe (topicName, qos)
Subscribes to a topic.

Parameters:

  • topicName string The name of the topic.
  • qos number The QoS.

Returns:

    a promise that resolves once the message is sent.
mqttClient:unsubscribe (topicName)
Unubscribes from a topic.

Parameters:

  • topicName string The name of the topic.

Returns:

    a promise that resolves once the message is sent.

Class MqttServer

The MqttServer class enables clients to exchange Application Messages using publish and subscribe.

Usage:

local event = require('jls.lang.event')
local mqtt = require('jls.net.mqtt')

local mqttServer = mqtt.MqttServer:new()
mqttServer:bind()

event:loop()
MqttServer:new ()
Creates a new MQTT server.

Returns:

    a new MQTT server
mqttServer:bind (node[, port[, backlog[, callback]]])
Binds this server to the specified address and port number.

Parameters:

  • node string the address, the address could be an IP address or a host name.
  • port number the port number, 0 to let the system automatically choose a port, default is 1883. (optional)
  • backlog number the accept queue size, default is 32. (optional)
  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the server is bound.
mqttServer:close ([callback])
Closes this server.

Parameters:

  • callback function an optional callback function to use in place of promise. (optional)

Returns:

    jls.lang.Promise a promise that resolves once the server is closed.
generated by LDoc 1.4.6