Modules@robinpath/mqtt
mqtt

@robinpath/mqtt

0.1.5Node.jsPublic

MQTT client module for connecting to MQTT brokers, publishing messages, subscribing to topics, and handling incoming messages. Supports multiple concurrent client connections, QoS levels, last will messages, and message history tracking.

MQTT

MQTT client module for connecting to MQTT brokers, publishing messages, subscribing to topics, and handling incoming messages. Supports multiple concurrent client connections, QoS levels, last will messages, and message history tracking.

Package: @robinpath/mqtt | Category: Web | Type: Utility

Authentication

mqtt.connect "your-credentials"

Call this once at the start of your script before using any other function. Credentials persist for the duration of the script execution.

Use Cases

Use the mqtt module when you need to:

  • Publish a message to an MQTT topic -- Use mqtt.publish to perform this operation
  • Subscribe to an MQTT topic -- Use mqtt.subscribe to perform this operation
  • Unsubscribe from an MQTT topic -- Use mqtt.unsubscribe to perform this operation
  • Register a message handler for incoming MQTT messages -- Use mqtt.on to perform this operation
  • Disconnect from an MQTT broker and clean up resources -- Use mqtt.disconnect to perform this operation

Quick Reference

FunctionDescriptionReturns
connectConnect to an MQTT brokerobject
publishPublish a message to an MQTT topicobject
subscribeSubscribe to an MQTT topicobject
unsubscribeUnsubscribe from an MQTT topicobject
onRegister a message handler for incoming MQTT messagesobject
disconnectDisconnect from an MQTT broker and clean up resourcesobject
isConnectedCheck if an MQTT client is currently connectedobject
reconnectReconnect an existing MQTT client to its brokerobject
topicsList all topics the client is currently subscribed toobject
lastMessageGet the last received message on a specific topicobject
qosSet the default Quality of Service level for the clientobject
willSet the last will and testament message for the clientobject

Functions

connect

Connect to an MQTT broker

Module: mqtt | Returns: object -- API response.

mqtt.connect
ParameterTypeRequiredDescription
idstringNoClient identifier
brokerUrlstringNoBroker URL (e.g. mqtt://localhost:1883)
optionsobjectNoMQTT connection options

publish

Publish a message to an MQTT topic

Module: mqtt | Returns: object -- API response.

mqtt.publish
ParameterTypeRequiredDescription
idstringNoClient identifier
topicstringNoTopic to publish to
messagestringNoMessage payload
optionsobjectNoPublish options (qos, retain, etc.)

subscribe

Subscribe to an MQTT topic

Module: mqtt | Returns: object -- API response.

mqtt.subscribe
ParameterTypeRequiredDescription
idstringNoClient identifier
topicstringNoTopic to subscribe to
optionsobjectNoSubscribe options (qos, etc.)

unsubscribe

Unsubscribe from an MQTT topic

Module: mqtt | Returns: object -- API response.

mqtt.unsubscribe
ParameterTypeRequiredDescription
idstringNoClient identifier
topicstringNoTopic to unsubscribe from

on

Register a message handler for incoming MQTT messages

Module: mqtt | Returns: object -- API response.

mqtt.on
ParameterTypeRequiredDescription
idstringNoClient identifier
handlerIdstringNoUnique handler identifier
callbackstringNoHandler function receiving (topic, message)

disconnect

Disconnect from an MQTT broker and clean up resources

Module: mqtt | Returns: object -- API response.

mqtt.disconnect
ParameterTypeRequiredDescription
idstringNoClient identifier

isConnected

Check if an MQTT client is currently connected

Module: mqtt | Returns: object -- API response.

mqtt.isConnected
ParameterTypeRequiredDescription
idstringNoClient identifier

reconnect

Reconnect an existing MQTT client to its broker

Module: mqtt | Returns: object -- API response.

mqtt.reconnect
ParameterTypeRequiredDescription
idstringNoClient identifier

topics

List all topics the client is currently subscribed to

Module: mqtt | Returns: object -- API response.

mqtt.topics
ParameterTypeRequiredDescription
idstringNoClient identifier

lastMessage

Get the last received message on a specific topic

Module: mqtt | Returns: object -- API response.

mqtt.lastMessage
ParameterTypeRequiredDescription
idstringNoClient identifier
topicstringNoTopic to get last message for

qos

Set the default Quality of Service level for the client

Module: mqtt | Returns: object -- API response.

mqtt.qos
ParameterTypeRequiredDescription
idstringNoClient identifier
levelnumberNoQoS level: 0 (at most once), 1 (at least once), 2 (exactly once)

will

Set the last will and testament message for the client

Module: mqtt | Returns: object -- API response.

mqtt.will
ParameterTypeRequiredDescription
idstringNoClient identifier
topicstringNoWill topic
payloadstringNoWill message payload
qosnumberNoWill QoS level (0, 1, or 2)
retainbooleanNoWhether to retain the will message

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Topic is required.Check the error message for details
A callback function is required.Check the error message for details
QoS level must be 0, 1, or 2.Check the error message for details
Topic is required for last will.Check the error message for details
MQTT client "..." not found. Call connect() first.Check the error message for details
MQTT client "..." already exists. Disconnect first or use a different id.Check the error message for details
@desc "Publish and validate result"
do
  set $result as mqtt.publish
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Multi-step MQTT workflow

Chain multiple mqtt operations together.

@desc "Connect, publish, and more"
do
  set $r_connect as mqtt.connect
  set $r_publish as mqtt.publish
  set $r_subscribe as mqtt.subscribe
  print "All operations complete"
enddo

2. Safe connect with validation

Check results before proceeding.

@desc "Connect and validate result"
do
  set $result as mqtt.connect
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.5latest1 months ago
Install
$ robinpath add @robinpath/mqtt

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.5
LicenseMIT
Unpacked Size9.1 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

web