Class: Mqtt

olympe.net. Mqtt

MQTT client implementation.


new Mqtt(url [, options])

Creates an instance of a MQTT client.

Parameters:
Name Type Argument Description
url string

the url for bus connection

options olympe.net.Mqtt.MQTTConnectionOptions <optional>

the connection options

Members


<static> Events :string

Mqtt events.

Type:
  • string
Properties:
Name Type Default Description
CONNECT string connect
MESSAGE string message
CLOSE string close
END string end

Methods


connect( [callback])

Creates a new connection to the bus.

Parameters:
Name Type Argument Description
callback function <optional>
Returns:
Type
olympe.net.Mqtt

end( [force] [, callback])

Closes the client.

Parameters:
Name Type Argument Description
force boolean <optional>

passing it to true will close the client right away, without waiting for the in-flight messages to be acked. Optional

callback function <optional>

will be called when the client is closed. Optional

Returns:
Type
olympe.net.Mqtt

isConnected()

Checks if the client is connected to the bus.

Returns:
Type
boolean

onClose(callback)

Registers a callback to be invoked on connection closed.

Parameters:
Name Type Description
callback function

the callback to invoke

Returns:
Type
olympe.net.Mqtt

publish(topic, message [, options] [, callback])

Publishes a message to the given topic.

Parameters:
Name Type Argument Description
topic string

the topic on which we will publish the message

message Object

the message

options olympe.net.Mqtt.PublishOptions <optional>

the publish options

callback function <optional>

the callback to invoke once the message is published

Returns:
Type
olympe.net.Mqtt

subscribe(topic, callback)

Subscribes to the given topic and starts receiving messages.

Parameters:
Name Type Description
topic string

the topic to subscribe to

callback function

the callback to invoke when a message is received from the given topic

Returns:
Type
olympe.net.Mqtt

unsubscribe(topic [, callback])

Unsubscribes from the given topic.

Parameters:
Name Type Argument Description
topic string

the topic to unsubscribe from

callback function <optional>

the callback to invoke when the unsubscription is complete. Optional

Returns:
Type
olympe.net.Mqtt

Type Definitions


MQTTConnectionOptions

Mqtt connection options.

Type:
  • Object
Properties:
Name Type Description
wsOptions olympe.net.Mqtt.WSOptions

is the WebSocket connection options. Default is {}. It's specific for WebSockets

keepalive number

60 seconds, set to 0 to disable

reschedulePings boolean

reschedule ping messages after sending packets (default true)

clientId string

'mqttjs_' + Math.random().toString(16).substr(2, 8)

clean boolean

true, set to false to receive QoS 1 and 2 messages while offline

reconnectPeriod number

1000 milliseconds, interval between two reconnections. Disable auto reconnect by setting to 0.

connectTimeout number

30 * 1000 milliseconds, time to wait before a CONNACK is received

username string

the username required by your bus, if any

password string

the password required by your bus, if any

queueQoSZero boolean

if connection is broken, queue outgoing QoS zero messages (default true)


PublishOptions

Publish options.

Type:
  • Object
Properties:
Name Type Description
qos number

QoS level, Number, default 0

retain boolean

retain flag, default false

dup boolean

mark as duplicate flag, default false


WSOptions

WebSocket connection options.

Type:
  • Object
Properties:
Name Type Description
followRedirects boolean

Whether or not to follow redirects. Defaults to false.

handshakeTimeout number

Timeout in milliseconds for the handshake request. This is reset after every redirection.

maxRedirects number

The maximum number of redirects allowed. Defaults to 10.

perMessageDeflate boolean | Object

Enable/disable permessage-deflate.

protocolVersion number

Value of the Sec-WebSocket-Version header.

origin string

Value of the Origin or Sec-WebSocket-Origin header depending on the protocolVersion.

maxPayload number

The maximum allowed message size in bytes.