Rsup MQTT
📢 Elegant wrapper for the paho mqtt client
Why
Paho is fast and light, but interface is not good. So I wrapped to better interface...
Install
yarn add rsup-mqtt
//es6; //commonjsvar connect = connect;
browser
Example
Basic
const client = await clientclient // => hello mqtt
API
connect(options)
Connects to the broker. Returns Promise<Client>
.
// or
Options
-
host
- Host address, required. -
port
- Defaults is443
or80
. -
path
- Defaults is'/'
. -
ssl
- Defaults isfalse
. -
clientId
Defaults is random string. -
hosts
- Optional. fallback urls. -
keepalive
Defaults is60
. -
username
Optional. -
password
Optional. -
cleanSession
Defaults isfalse
. -
reconnect
Defaults istrue
. -
mqttVersion
Defaults is4
. -
mqttVersionExplicit
Default value istrue
if the "mqttVersion" is 4, and false otherwise. -
will
Optional.topic
Required.payload
Required.qos
Defaults is0
retain
Defaults isfalse
.
-
Constructor
- SeeConstructor
Constructor
If want to extend Client.
...
Client
client.isConnected()
Returns whether the client is connected.
client.on(eventName, listener)
Add an event listener.
Events
- message - When a message is received.
- sent - When a message is sent.
- close - When a connection is completely closed.
- error - When an error occurs.
- reconnect - When start reconnecting.
message
| sent
Message type event - Receive topic and message. See details about Message.
client client// => "topic", "hello~"
close
| error
| reconnect
Error type event - Receive error or not. See details about Error.
client
client.once(eventName, listener)
Add an event listener. Runs once.
client.off(eventName [, listener])
Remove the event listener(s).
clientclient // If no listener, remove all,
client.onMessage(topic, listener)
Add an listener for received message.
client
client.onSent(topic, listener)
Add an listener for sent message.
client.removeMessageListener(topic [, listener])
Remove the listener(s) for received message.
client.removeSentListener(topic [, listener])
Remove the listener(s) for sent message.
client.subscribe(topic)
Subscribe to a topic. Returns subscription instance. See details about Subscription.
client.unsubscribe(topic [, removeListeners])
Unsubscribe from a topic. If removeListeners
is true, remove all the topic listeners.
client.subscribed()
Returns an array of subscribed topic.
clientclient console// => ['topic1', 'topic2']
client.publish(topic, payload [, options])
Publish a message to a topic.
client // string messageclient // Convert object to json string.client // buffer message
options
qos
Defaults is0
.retain
Defaults isfalse
.
client.send(topic, payload [, options])
Alias client.publish()
.
client.disconnect()
Disconnect the connection.
client.reconnect()
Connect again using the same options. Returns Promise<void>
.
Subscription
subscription.topic
Subscribed topic.
const subscription = client console// => topic
subscription.on(listener)
Add an listener for received topic message.
subscription.off([listener])
Remove the topic listener(s).
subscription.publish(payload [, options])
Publish a message to topic.
subscription.send(payload [, options])
Alias subscription.publish()
.
subscription.unsubscribe([removeListeners])
Unsubscribe from a topic. If removeListeners
is true, remove all the topic listeners.
Message
message.topic
Message's topic.
message.string
A payload of string type.
message.json
A payload of json type.
message.bytes
A payload of buffer type.
message.qos
Message's qos. Returns number.
message.retain
Message's retain. Returns boolean.
message.dup
Duplicate Message or not. Returns boolean.
Error
error.code
Error's code number.
error.message
Error's message
error.occurred()
Error occurred or not. Returns boolean.
error.is(code)
Compare error code number. Returns boolean.
console// => true or false
Error Codes
OK
- No error.CONNECT_TIMEOUT
- Connect timed out.SUBSCRIBE_TIMEOUT
- Subscribe timed out.UNSUBSCRIBE_TIMEOUT
- Unsubscribe timed out.PING_TIMEOUT
- Ping timed out.INTERNAL_ERROR
- Internal error.CONNACK_RETURNCODE
- Bad Connack return code.SOCKET_ERROR
- Socket error.SOCKET_CLOSE
- Socket closed.MALFORMED_UTF
- Malformed UTF data.UNSUPPORTED
- Not supported by this browser.INVALID_STATE
- Invalid state.INVALID_TYPE
- Invalid type.INVALID_ARGUMENT
- Invalid argument.UNSUPPORTED_OPERATION
- Unsupported operation.INVALID_STORED_DATA
- Invalid data in local storage.INVALID_MQTT_MESSAGE_TYPE
- Invalid MQTT message type.MALFORMED_UNICODE
- Malformed Unicode string.BUFFER_FULL
- Message buffer is full.
License
MIT