Error while using mqtt.js in meteor

Hi,

I have been trying to use mqtt.js from npm within meteor application. I am directly using mqtt using meteor npm install mqtt and than importing it on the client i.e. browser. Now when i use it to connect with the test server i am getting the following error.

modules.js?hash=97f460f…:67395 GET http://test.mosquitto.org/ net::ERR_CONNECTION_TIMED_OUT
modules.js?hash=97f460f…:134661 Uncaught TypeError: req.abort is not a function(…)
modules.js?hash=97f460f…:67391 Refused to set unsafe header "sec-websocket-version"
modules.js?hash=97f460f…:67391 Refused to set unsafe header "sec-websocket-key"
modules.js?hash=97f460f…:67391 Refused to set unsafe header “sec-websocket-extensions”

Any idea as to what i am doing wrong. The mqtt npm package read me says that i have to use browserify or webpack to create a library for browser but i am assuming that meteor build tool does that for you or am I mistaken.

Thanks

1 Like

GET http://test.mosquitto.org/ net::ERR_CONNECTION_TIMED_OUT
maybe their website is down

Nope it is working with node. I think somehow meteor build is not building correct bundle for browser in this case as http protocol should not be used. Either ws or mqtt should be used.

why are you not using / forking an existing package (search mqtt in atmosphere) , such as https://github.com/mkarliner/meteor-mqtt (server and client side) ?

Yeah the Meteor build tool should work exactly the same way as Browserify or Webpack. Could you make an example app someone can run to reproduce the issue?

I will try to create an example. I do not want to use atmosphere package as it internally just exposes mqtt from browserified bundle of mqtt npm repository.

1 Like

@suhaila what do you expect to do differently if not just “just” expose mqtt from npm repository ?

can you add this test : if (req) req.abort ?

Were you ever able to solve this problem?

I just used Paho client instead of mqttjs

Thanks for your response. I discovered Paho after making this forum post, and it works.

Hi,

Another workaround, for the ones who really need to use meteor-mqtt, would be just to create a server method, and just call it from the client.
Example:

// on server side
if (Meteor.isServer) {
    var mqtt = require('mqtt');
    var client = mqtt.connect('mqtt://domain');
    Meteor.methods({
        publishChange:function(data) {
            client.publish(data.topic, data.message);
        }
    });
}
...
// on client side
Meteor.call('publishChange', {'topic' : 'test-topic', 'message' : 'Hello world'});