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------| |
+---------------+ +---------------+
- node.js (>= 6)
- mqtt broker (on either your server or cloud like NIFTY Cloud or CloudMQTT)
- Open Server Page
- Click "Client Page" button to open Client page corresponding to the Server Page.
- Check console of both pages and click "Request" button.
- 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}`);
- supporting multiple server response
Apache License Version 2