-
Notifications
You must be signed in to change notification settings - Fork 18
Open
Description
How to use it with express and socketio ?
package.json
{
"name": "mqtt-device-simulator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"mqtt-scripts": "^1.2.2",
"express": "^4.17.1",
"socket.io": "^2.3.0"
},
"devDependencies": {},
"scripts": {
"start": "node index.js && mqtt-scripts -u 'mqtt://10.99.253.133' -v 'debug' -d ./",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Austin",
"license": "ISC"
}
index.js
var mqtt = require('mqtt-scripts')
var io = require("socket.io");
var express = require("express");
var app = express();
app.use(express.static('www'));
var server = app.listen(3333);
var webSockerIO = io.listen(server);
const deviceName = "Kiosk-1";
let orderId = 0
let orderDetail = "nothing"
webSockerIO.on('connection', function (socket) {
mqtt.subscribe("device/kiosk/2", (topic, val) => {
console.log(topic)
console.log(val)
var data = val;
if (data.method === "set") {
switch (data.cmd) {
case "orderId":
orderId = data.orderId;
socket.emit('mqtt', { 'msg': {'orderId': orderId} });
break;
case "orderDetail":
orderDetail = data.orderDetail;
break;
}
} else {
switch (data.cmd) {
case "ping":
socket.emit('mqtt', { 'msg': 'pong' });
data.ping = "pong";
break;
case "orderId":
data.orderId = orderId;
break;
case "order":
data.order = deviceName;
break;
}
}
mqtt.publish("ResponseTopic", JSON.stringify(data));
});
});
error
> mqtt-device-simulator@1.0.0 start /Users/austin/Documents/company/edgeX/mqtt-device-simulator-kiosk
> node index.js && mqtt-scripts -u 'mqtt://10.99.253.133' -v 'debug' -d ./
2020-08-31 18:21:29.243 <info> mqtt-scripts 1.2.2 starting
2020-08-31 18:21:29.581 <info> mqtt connected mqtt://127.0.0.1
/Users/austin/Documents/company/edgeX/mqtt-device-simulator-kiosk/index.js:17
mqtt.subscribe("device/kiosk/2", (topic, val) => {
^
TypeError: mqtt.subscribe is not a function
at Namespace.<anonymous> (/Users/austin/Documents/company/edgeX/mqtt-device-simulator-kiosk/index.js:17:10)
at Namespace.emit (events.js:315:20)
at Namespace.EventEmitter.emit (domain.js:482:12)
at Namespace.emit (/Users/austin/Documents/company/edgeX/mqtt-device-simulator-kiosk/node_modules/socket.io/lib/namespace.js:213:10)
at /Users/austin/Documents/company/edgeX/mqtt-device-simulator-kiosk/node_modules/socket.io/lib/namespace.js:181:14
at processTicksAndRejections (internal/process/task_queues.js:79:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! mqtt-device-simulator@1.0.0 start: `node index.js && mqtt-scripts -u 'mqtt://10.99.253.133' -v 'debug' -d ./`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the mqtt-device-simulator@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/austin/.npm/_logs/2020-08-31T10_21_32_982Z-debug.log
Metadata
Metadata
Assignees
Labels
No labels