Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use it with express and socketio #12

Open
augustine-lin opened this issue Aug 31, 2020 · 0 comments
Open

How to use it with express and socketio #12

augustine-lin opened this issue Aug 31, 2020 · 0 comments

Comments

@augustine-lin
Copy link

augustine-lin commented Aug 31, 2020

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

> [email protected] 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! [email protected] 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 [email protected] 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant