Module jls.net.mqtt

This module provide classes to work with MQTT.

Message Queuing Telemetry Transport see http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html

Class MqttClient

MqttClient:new () Creates a new MQTT client.
mqttClient:connect ([addr[, port]]) Connects this MQTT client.
mqttClient:publish (topicName, payload) Publishes a message payload on a topic.
mqttClient:close () Connects this MQTT client.
mqttClient:subscribe (topicName, qos) Subscribes to 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:publish (topicName, payload)
Publishes a message payload on a topic.

Parameters:

  • topicName string The name of the topic.
  • payload string The message payload.

Returns:

    a promise that resolves once the message is sent.
mqttClient:close ()
Connects this MQTT client.

Returns:

    a promise that resolves once the client is closed.
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.

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.
  • 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 Last updated 2022-01-22 16:32:56