Skip to content
This repository was archived by the owner on Feb 11, 2020. It is now read-only.

MQTT over Websockets

SudoPlz edited this page May 4, 2014 · 7 revisions

Since v0.13.0 Mosca support MQTT over Websocket through the mows package.

It is very easy to use:

Client side: (just an index.html file to make the calls)

<html>
  <head>
    <script src="/mqtt.js"></script>
  </head>
  <body>
    <script>
      var client = mqtt.createClient();

      client.subscribe("mqtt/demo");

      client.on("message", function(topic, payload) {
        alert([topic, payload].join(": "));
        client.end();
      });

      client.publish("mqtt/demo", "hello world!");
    </script>
  </body>
</html>

Server side: We augment any node application with MQTT-over-websocket capabilities, just by calling the Server#attachHttpServer method, like so:

var http     = require('http)
  , httpServ = http.createServer()
  , mosca    = require('mosca')
  , mqttServ = new mosca.Server();

mqttServ.attachHttpServer(httpServ);

httpServer.listen(3000);

It is also possible to serve the browserified bundle for the mqtt client:

var http     = require('http')
  , express  = require('express')
  , app      = express();
  , httpServ = http.createServer(app)
  , mosca    = require('mosca')
  , mqttServ = new mosca.Server();

mqttServ.attachHttpServer(httpServ);
mqttServ.serveBundle(app);

httpServer.listen(3000);

###OR if we run the standalone

just serve it with Mosca:

$ mosca --http-port 3000 --http-static . --http-bundle \
        --very-verbose | bunyan

And point your browser to http://localhost:3000.

Clone this wiki locally