WebSockets Interface
Device Cloud provides a WebSockets interface to enable the use of the MQTT protocol to interact with the platform from within a browser.
MQTT over Websockets
-
supports WebSockets as defined by RFC 6455
-
use mqttv3.1 as the WebSocket protocol name
-
to connect, use the HTTP and HTTPS endpoints listed on the Developer page on the Management Portal (endPointURL).
-
MQTT: ws://endPointURL/mqtt
-
MQTT with TLS/SSL encryption: wss:/endPointURL//mqtt-ssl
-
-
Allows usage of all the normal MQTT interface functionality including the TR50 commands and additional Server Topics.
Paho Javascript Client
Example connecting to the WebSockets interface over MQTT with SSL:
var client = new Paho.MQTT.Client('api.devicecloud.windriver.com', 443, '/mqtt-ssl', 'clientId-or-appId');
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({
userName : 'thingKey',
password : 'appToken',
useSSL: true,
mqttVersion : 3,
onSuccess : onConnect,
onFailure : onFailure
});
function onConnect() {
console.log('Connected.');
client.subscribe('reply');
var cmd = JSON.stringify({
"cmd": {
"command": "diag.ping"
}
});
var message = new Paho.MQTT.Message(cmd);
message.destinationName = 'api';
client.send(message);
}
function onFailure(responseObject) {
console.log('Failed to connect: ' + responseObject.errorMessage);
}
function onConnectionLost(responseObject) {
console.log('Connection lost: ' + responseObject.errorMessage);
}
function onMessageArrived(message) {
console.log(message.destinationName + ': ' + message.payloadString);
}