/mqttpress

Serverless API Server emulated by MQTT

Primary LanguageJavaScript

mqttpress

MQTTPRESS is another Serverless Architecture using MQTT Broker.

+--------+
|  MQTT  |
+--------+
 |  |   |
 |  |   +--------------------------------------+
 |  |                                          |
 |  +------------------+                       |
 |                     |                       |
+---------------+    +-------------------+    +------------------+
|  Server Page  |    |  Client Page (1)  |    |  Client Page (2) |
+---------------+    +-------------------+    +------------------+

emulates this!

+---------------+               +---------------+
|  Client Page  |------Req----->|  Server Page  |
|      (1)      |<-----Res------|               |
+---------------+	            |               |
                                |               |
+---------------+               |               |
|  Client Page  |------Req----->|               |
|      (2)      |<-----Res------|               |
+---------------+	            +---------------+

Requirements

Demo

  1. Open Server Page
  2. Click "Client Page" button to open Client page corresponding to the Server Page.
  3. Check console of both pages and click "Request" button.

How to use

  • MQTT on Docker
% docker run -d -p 1883:1883 -p 9001:9001 --name=mosquitto sourceperl/mosquitto
  • Server Side
const mqttpress = require("mqttpress");
const config = require("./config");
const debug = require("debug")("server");

const app = mqttpress();

app.hear("news/#", (res)=>{
  debug(`hear: ${res.topic}, from: ${JSON.stringify(res.from)}, data: ${JSON.stringify(res.data)}`);
  res.send({msg: `congrat ${res.data.winner} win!`});
});

app.hear("project/+/news", (res)=>{
  debug(`hear: ${res.topic}, from: ${JSON.stringify(res.from)}, data: ${JSON.stringify(res.data)}`);
  res.send({msg: `congrat ${res.data.loser} lose!`});
});

app.on("listening", ()=>{
  debug(`listen ${app.id}`);
});

app.on("error", (err)=>{
  console.error(err.stack);
});

app.listen(`ws://${config.mqtt.host}:${config.mqtt.ports.ws}`);
  • Client Side
const mqttpress = require("mqttpress");
const config = require("./config");
const debug = require("debug")("client");

const app = mqttpress();
app.on("connect", ()=>{
  debug(`connection ready ${app.id}`);

  debug(`send:`);
  app.send("news/sports", {winner: "tigers"}).then((data)=>{
    debug(`resv: ${JSON.stringify(data)}`);
  }).catch((err)=>{
    console.error(err.stack);
  });

  app.send("project/sports/news", {loser: "giants"}).then((data)=>{
    debug(`resv: ${JSON.stringify(data)}`);
  }).catch((err)=>{
    console.error(err.stack);
  });
});

app.connect(`ws://${config.mqtt.host}:${config.mqtt.ports.ws}`);

TODO

  • supporting multiple server response

License

Apache License Version 2