Update to MQTT.js (MQTT Client) and CDN

250 views
Skip to first unread message

Julian Knight

unread,
Nov 14, 2016, 4:06:16 AM11/14/16
to Node-RED
Hi all, for anyone is using the MQTT.JS client from Matteo Collina, which gives you both a Node.JS and a Browser based library for accessing MQTT over websockets.

I asked him to pre-build the browser versions when he does releases and he updated the code straight away & released a new version (v2.1.1).

This means that, if you want to use the library in a web page generated from Node-RED, you have the choice of deployments:
  • Use a CDN version. https://unpkg.com/mqtt/dist/mqtt.min.js gets you the latest version or you can specify a specific version, https://unpkg.com/mq...@2.1.1/dist/mqtt.min.js.
    The advantage is that you don't have to install anything locally. The downside is that you need an Internet connection.

  • Install via npm: npm install mqtt --save or yarn: yarn add mqtt. Assuming you installed this with your other Node-RED modules in ~/.node-red, you should then copy ~/.node-red/node_modules/mqtt/dist/mqtt.min.js to your static folder and reference appropriately in the web template.
    The advantage of this is that you only need the internet to install the library (which you could manually copy over since there is all pure JavaScript) to get a web.

Worth noting that the unpkg.com side makes ALL npm libraries available from a CDN automatically, check out the site for details, it's great for development. Here are a couple of other examples:

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/persistent-ws"></script>

The first gives you the superb Vue framework (like Angular or REACT but much easier to use, smaller and faster). The second is an auto-reconnecting websocket library. Note that the second library doesn't have a build step, it is already browser-ready and so doesn't have a "dist" folder.

Hope this helps. JK.
Reply all
Reply to author
Forward
0 new messages