Losant JavaScript MQTT Client in the Browser?

#1

Can I use losant-mqtt,js in my browser? Can I import it as use it as part of the JavaScript on a webpage?

#2

I haven’t confirmed it myself, but losant-mqtt is a wrapper around MQTT.js, which is supported in the browser: https://github.com/mqttjs/MQTT.js#browser

So with something like Webpack or Browserify, you should be able to use losant-mqtt in the browser. Give it a try and let us know!

#3

Brandon,

Thanks! will do. If I can get it to work,

Greg

#4

Worst-case scenario, our underlying mqtt protocol is pretty straightforward, so you could use MQTT.js directly in the browser.

#5

Greg, I just confirmed that it works.

Check out this gist: https://gist.github.com/anaptfox/708cd010a53d19b7b7dc029a235ca092

The key is to set the transport to web sockets. I did that here:

Just add transport: 'ws' to the Device options.

#6

You can also test things out pretty quickly with: RequireBin.com

#7

Brandon,

Thanks! I had some thoughts along the same line, just not sure how to use the Losant without the wrapper. A bit new to this IoT and MQTT game. Amazingly I had the weather station example up connected to a slack channel AND my edison sending data to Losant in a matter of a couple of hours. Losant is pretty amazing.

Greg

1 Like
#8

anaptfox,

Thanks! Will try as soon as I can!

Greg

1 Like
#9

No Problem! Please reach out if you have more questions.