new Client(host, port, path, clientId)
Most applications will create just one Client object and then call its connect() method, however applications can create more than one Client object if they wish. In this case the combination of host, port and clientId attributes must be different for each Client object.
The send, subscribe and unsubscribe methods are implemented as asynchronous JavaScript methods (even though the underlying protocol exchange might be synchronous in nature). This means they signal their completion by calling back to the application, via Success or Failure callback functions provided by the application on the method in question. Such callbacks are called at most once per method invocation and do not persist beyond the lifetime of the script that made the invocation.
In contrast there are some callback functions, most notably onMessageArrived, that are defined on the Paho.MQTT.Client object. These may get called multiple times, and aren't directly related to specific method invocations made by the client.
Parameters:
Name | Type | Description |
---|---|---|
host |
string | the address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address. |
port |
number | the port number to connect to - only required if host is not a URI |
path |
string | the path on the host to connect to - only used if host is not a URI. Default: '/mqtt'. |
clientId |
string | the Messaging client identifier, between 1 and 23 characters in length. |
Properties:
Name | Type | Description |
---|---|---|
host |
string | read only the server's DNS hostname or dotted decimal IP address. |
port |
number | read only the server's port. |
path |
string | read only the server's path. |
clientId |
string | read only used when connecting to the server. |
onConnectionLost |
function | called when a connection has been lost.
after a connect() method has succeeded.
Establish the call back used when a connection has been lost. The connection may be
lost because the client initiates a disconnect or because the server or network
cause the client to be disconnected. The disconnect call back may be called without
the connectionComplete call back being invoked if, for example the client fails to
connect.
A single response object parameter is passed to the onConnectionLost callback containing the following fields:
|
onMessageDelivered |
function | called when a message has been delivered.
All processing that this Client will ever do has been completed. So, for example,
in the case of a Qos=2 message sent by this client, the PubComp flow has been received from the server
and the message has been removed from persistent storage before this callback is invoked.
Parameters passed to the onMessageDelivered callback are:
|
onMessageArrived |
function | called when a message has arrived in this Paho.MQTT.client.
Parameters passed to the onMessageArrived callback are:
|
onConnected |
function | called when a connection is successfully made to the server.
after a connect() method.
Parameters passed to the onConnected callback are:
|
disconnectedPublishing |
boolean | if set, will enable disconnected publishing in in the event that the connection to the server is lost. |
disconnectedBufferSize |
number | Used to set the maximum number of messages that the disconnected buffer will hold before rejecting new messages. Default size: 5000 messages |
trace |
function | called whenever trace is called. TODO |
- Source:
Methods
connect(connectOptions)
Parameters:
Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
connectOptions |
object | Attributes used with the connection.
Properties
|
- Source:
Throws:
-
If the client is not in disconnected state. The client must have received connectionLost or disconnected before calling connect for a second or subsequent time.
- Type
- InvalidState
disconnect()
- Source:
Throws:
-
if the client is already disconnected.
- Type
- InvalidState
getTraceLog() → {Array.<Object>}
- Source:
Returns:
- Type
- Array.<Object>
publish(topic, payload, qos, retained)
Parameters:
Name | Type | Description |
---|---|---|
topic |
string | Paho.MQTT.Message | mandatory The name of the topic to which the message is to be published. - If it is the only parameter, used as Paho.MQTT.Message object. |
payload |
String | ArrayBuffer | The message data to be published. |
qos |
number | The Quality of Service used to deliver the message.
|
retained |
Boolean | If true, the message is to be retained by the server and delivered to both current and future subscriptions. If false the server only delivers the message to current subscribers, this is the default for new Messages. A received message has the retained boolean set to true if the message was published with the retained boolean set to true and the subscrption was made after the message has been published. |
- Source:
Throws:
-
if the client is not connected.
- Type
- InvalidState
send(topic, payload, qos, retained)
Parameters:
Name | Type | Description |
---|---|---|
topic |
string | Paho.MQTT.Message | mandatory The name of the destination to which the message is to be sent. - If it is the only parameter, used as Paho.MQTT.Message object. |
payload |
String | ArrayBuffer | The message data to be sent. |
qos |
number | The Quality of Service used to deliver the message.
|
retained |
Boolean | If true, the message is to be retained by the server and delivered to both current and future subscriptions. If false the server only delivers the message to current subscribers, this is the default for new Messages. A received message has the retained boolean set to true if the message was published with the retained boolean set to true and the subscrption was made after the message has been published. |
- Source:
Throws:
-
if the client is not connected.
- Type
- InvalidState
startTrace()
- Source:
stopTrace()
- Source:
subscribe(filter, subscribeOptions)
Parameters:
Name | Type | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
filter |
string | describing the destinations to receive messages from.
|
||||||||||||||||||
subscribeOptions |
object | used to control the subscription
Properties
|
- Source:
Throws:
-
if the client is not in connected state.
- Type
- InvalidState
unsubscribe(filter, unsubscribeOptions)
Parameters:
Name | Type | Description | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
filter |
string | describing the destinations to receive messages from. | |||||||||||||||
unsubscribeOptions |
object | used to control the subscription
Properties
|
- Source:
Throws:
-
if the client is not in connected state.
- Type
- InvalidState