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.
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.
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.
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'});