The library is designed to be formally equivalent to the Eclipse Paho JavaScript Client as much as possible, even if some differences still apply, especially for what concerns the usage of namespaces and the way a connection to the broker is established.
Following is an example showing some basic operations, such as getting a connection, subscribing to a topic and publishing a message:
// Connect to the MQTT.Cool server hosted at 'my.push.server' and listening on tcp port 8080. mqttcool.openSession('http://my.MQTT.Cool.server:8080', 'my_user', 'my_password', { onConnectionSuccess: function(mqttCoolSession) { // Get a client instance, which will connect to the MQTT broker mapped by // the alias "my_mqtt_broker" using the clientId 'my_client_id'. var client = mqttCoolSession.createClient('my_mqtt_broker', 'my_client_id'); // Set function callbacks. client.onConnectionLost = onConnectionLost; client.onMessageArrived = onMessageArrived; // Connect to the MQTT broker. client.connect( { onSuccess: onConnect } ); // Called when the connection has been successfully acknowledged. function onConnect() { console.log("onConnect"); // Subscribe to the topic '/World' client.subscribe("/World"); // Publish the message 'Hello' to the topic '/World'. message = new Message("Hello"); message.destinationName = "/World"; client.send(message); } // Called when the client loses its connection. function onConnectionLost(responseObject) { if (responseObject.errorCode !== 0) { console.log("onConnectionLost:" + responseObject.errorMessage); } } // Called upon receiving a message. function onMessageArrived(message) { console.log("onMessageArrived:" + message.payloadString); } } });
To learn more about client application development, please refer to the Chapter "Client Application Development" of MQTT.Cool Getting Started Guide.