Question

I have a Worklight project that uses Paho MQTT Javascript 'mqttws31.js'.

In the simulator everything works fine, but when connecting from a device it always gives me a timeout.

When I connect to the broker through the app called 'MyMQTT' on the device it connects fine. I can't get it to work with Javascript.

I tested if websockets are supported with this code - Link The webview in the application supports websockets, so that could not be the problem.

I tried several connection options, but it always times out.

Are there some Android permissions I have to add?

I added one of the examples I tried below.

The broker that I use is HiveMQ and I enabled websocket support on port 8000.

I also tried to connect to a public broker - test.mosquitto.org", 80 But that gives me the same results (works in simulator, not on device).

Please help me!

I know that the Cordova plugin would be better than the Javascript version, but I don't find the Cordove MQTT plugin for Cordova 3.1. That could help me out to.


    var client = new Messaging.Client("192.168.137.2", 8000, "prototype2");
    $(function() {
        $("#publishBtn").click(function() {
            publish("test", "prototype2/testpublish", 1);
        });

        var options = {

            // connection attempt timeout in seconds
            timeout : 5,

            // Gets Called if the connection has successfully been established
            onSuccess : function() {
                $("#console").append(
                        '<br/>' + "Connected to MQTT Broker over Websockets.");

                // client.subscribe("testtopic", {qos: 2});
                client.subscribe("prototype3/testpublish");
            },

    // Gets Called if the connection could not be established
    onFailure : function(message) {
        $("#console").append(
                '<br/>' + "Connection failed: " + message.errorMessage);
    }
};
// Gets called whenever you receive a message for your subscriptions
client.onMessageArrived = function(message) {

    var topic = message.destinationName;

    var message = message.payloadString;

    $("#console").append(
            '<br/>' + 'Message arrived: Topic: ' + topic + ', Message: '
                    + message);

};

// Attempt to connect
client.connect(options);

});

Was it helpful?

Solution

For Android 4.3 and lower, you need a cordova plug-in to provide WebSockets. There are a number of these around, but most don't implement the features required by MQTT (support for sub-protocols and for binary messages)

One that does is: https://github.com/mkuklis/phonegap-websocket. With that plug in, mqttws31.js works fine on Android 4.3.

There is currently an issue with installing standard Cordova 3 plugins in Worklight 6.1, which means that when you add the plug-in to your project, you will ned to have to edit the JavaScript provided on Github. The edit is simple, just change: require() to: cordova.require()

OTHER TIPS

I would suggest that you use the develop branch of Paho JS client.

The code below is what I've developed for the Eclipse IoT website demo ; it works just fine on my Android phone (4.4 though)

var client = new Messaging.Client("ws://iot.eclipse.org/ws", "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("/fosdem/#");
};

function onConnectionLost(responseObject) {
    if (responseObject.errorCode !== 0)
        console.log("onConnectionLost:" + responseObject.errorMessage);
};

function onMessageArrived(message) {
    console.log("onMessageArrived: "+message.destinationName +": " +message.payloadString);
    // my stuff ...
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top