This programming interface lets a JavaScript client application use the MQTT V3.1 or V3.1.1 protocol to connect to an MQTT-supporting messaging server. The function supported includes:
- Connecting to and disconnecting from a server. The server is identified by its host name and port number.
- Specifying options that relate to the communications link with the server, for example the frequency of keep-alive heartbeats, and whether SSL/TLS is required.
- Subscribing to and receiving messages from MQTT Topics.
- Publishing messages to MQTT Topics.
The API consists of two main objects:
- Paho.MQTT.Client
- This contains methods that provide the functionality of the API, including provision of callbacks that notify the application when a message arrives from or is delivered to the messaging server, or when the status of its connection to the messaging server changes.
- Paho.MQTT.Message
- This encapsulates the payload of the message along with various attributes associated with its delivery, in particular the destination to which it has been (or is about to be) sent.
The programming interface validates parameters passed to it, and will throw an Error containing an error message intended for developer use, if it detects an error with any parameter.
Example:
client = new Paho.MQTT.Client(location.hostname, Number(location.port), "clientId");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
};
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0)
console.log("onConnectionLost:"+responseObject.errorMessage);
};
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
client.disconnect();
};
- Source: