node.js + mqtt (Docker)

Directory Path : /app

Dockerfile

FROM node:latest
# 
WORKDIR specifies the directory our
# application's code will live within
WORKDIR /app
# We copy our package.json file to our
# app directory

RUN npm install -g nodemon

COPY package.json /app/package.json
# We then run npm install to install
# express for our application
RUN npm install && npm ls
RUN mv /app/node_modules /node_modules

# We then copy the rest of our application
# to the app direcoty
COPY . /app
# We start our application by calling
# npm start.
CMD ["npm", "start"]

mqtt-server.js

var mqtt = require('mqtt');
var fs = require('fs');
var path = require('path');

var clientID = "HKOSub"+Math.random().toString(36).substring(7);

var TRUSTED_CA_LIST = fs.readFileSync(path.join(__dirname, './key.pem'));

var address = '127.0.0.1';
var PORT = 1883;

const options = {
    host: address,
    port: PORT,
    keepalive: 60,
    clientId: clientID,
    // username: "testing_user",
    // password: "password",
    // protocol: 'mqtts',
    // rejectUnauthorized: true,
    // ca: TRUSTED_CA_LIST
};

var client = mqtt.connect(options);
var topic="name/#"
client.subscribe(topic, {  //subscribe to /[SI]/[DI]/raw/json
    qos: 1
});

client.on('message', function (topic, message) {
    console.log("[Received] topic: " + topic.toString());  //Print topic name
    console.log("[Received] message: " + message.toString()); //Print payload
})

function publishdata() {
  client.publish('LEUNG/test/time', 'Current time is: ' + new Date());
}

//setInterval(publishdata, 5000); //publish data every 10 seconds

package.json

{
    "name": "mqtt",
    "version": "1.0.0",
    "main": "mqtt-server.js",
    "dependencies": {
      "express" : "^4.16.3",
      "mqtt" : ""
    },
    "scripts": {
      "start": "node mqtt-server.js"
    }
  }

Docker

Docker build -t mqtt-server .

docker run -it -p 9001:3000 -v $(pwd):/app mqtt-server

56 thoughts on “node.js + mqtt (Docker)

  1. Pingback: Viagra or viagra

  2. Pingback: cialis daily cost

  3. Pingback: cialis black

  4. Pingback: cialis on line

  5. Pingback: Canadian viagra 50mg

  6. Pingback: Canadian pharmacy viagra

  7. Pingback: cialis from canada

  8. Pingback: how to get cialis

  9. Pingback: generic cialis cost

  10. Pingback: cialis dosage 40 mg

  11. Pingback: levitra vs cialis

  12. Pingback: viagra generic

  13. Pingback: viagra for sale

  14. Pingback: online pharmacy viagra

  15. Pingback: generic ed pills

  16. Pingback: non prescription ed pills

  17. Pingback: best non prescription ed pills

  18. Pingback: walmart pharmacy

  19. Pingback: cialis visa

  20. Pingback: levitra pills

  21. Pingback: levitra coupon

  22. Pingback: vardenafil 10mg

  23. Pingback: best online casino

  24. Pingback: hard rock casino online

  25. Pingback: buy viagra online cheap

  26. Pingback: legitimate online slots for money

  27. Pingback: online casino real money us

  28. Pingback: instant loans

  29. Pingback: cash advance

  30. Pingback: payday loans online

  31. Pingback: viagra 100mg

  32. Pingback: cialis 5 mg

  33. Pingback: 20 cialis

  34. Pingback: cialis 5 mg

  35. Pingback: cialis generic

  36. Pingback: 20 cialis

  37. Pingback: best online casino for money

  38. Pingback: best online casino real money

  39. Pingback: real money casino games

  40. Pingback: sildenafil viagra

  41. Pingback: viagra prices

  42. Pingback: natural viagra

  43. Pingback: buy cialis online cheap

  44. Pingback: safe viagra online

  45. Pingback: real viagra

  46. Pingback: sildenafil 20 mg online

  47. Pingback: 5mg cialis

  48. Pingback: viagra gel uk

  49. Pingback: generic sildenafil without a prescription

  50. Pingback: Fda approved viagra

  51. Pingback: viagra brand coupon

  52. Pingback: buy levitra online in usa

  53. Pingback: sildenafil 20 mg price in india

  54. Pingback: Order viagra us

  55. Pingback: men viagra

  56. Pingback: Overnight canadian viagra

發表迴響