mosca
.mosca
publisher.js
file belowsubscriber.js
file belowMQTT.js
package for Node.JS.Dockerfile
and copy/paste/adapt the code belowDockerfile
content for copy/paste :FROM ubuntu:18.04
MAINTAINER Radu Simen <radu103@hotmail.com>
RUN ln -fs /usr/share/zoneinfo/Europe/Bucharest /etc/localtime
RUN apt-get update
RUN apt-get install -y nodejs npm
RUN apt-get install -y make gcc g++ python git gyp build-essential
RUN npm install -g npm@latest
RUN node -v
RUN npm -v
RUN npm install -g node-gyp@latest
RUN chmod -R 777 /root
RUN chmod -R 777 /usr/local/lib
RUN npm install -g mosca bunyan
EXPOSE 80
ENTRYPOINT ["mosca", "--http-port", "80", "--http-bundle", "-v"]
Dockerfile
and run :docker login
docker build ./ -t radu103/mosca:latest -t radu103/mosca:1.0.0`
docker push radu103/mosca:1.0.0
docker push radu103/mosca:latest
docker run radu103/mosca:latest
docker ps
Kitematic
UI to see containers and images.cf login
cf push <APP_NAME> -m 256M --docker-image radu103/mosca:latest
cf logs <APP_NAME> --recent
https://<APP_URL>/mqtt.js
(because mosca is started with --http-bundle flag)package.json
{
"name": "stm-mqtt-broker",
"version": "1.0.0",
"description": "",
"main": "publisher.js",
"engines": {
"node": "^8.1.3",
"npm": "^6.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"mqtt": "^2.18.3"
}
}
publisher.js
var mqtt = require('mqtt');
var mqttHost = 'wss://<APP_NAME>.cfapps.eu10.hana.ondemand.com:443';
var topicName = 'myTopic';
var client = mqtt.connect(mqttHost);
client.on('connect', function () {
setInterval(function() {
var date = new Date();
var message = 'Hello mqtt ' + date.getTime();
client.publish(topicName, message);
date = new Date();
console.log(date.getTime() + ' - Message Sent : ' + message);
}, 5000);
});
client.on('error', function(err) {
console.log(err);
});
subscriber.js
var mqtt = require('mqtt');
var mqttHost = 'wss://<APP_NAME>.cfapps.eu10.hana.ondemand.com:443';
var topicName = 'myTopic';
var client = mqtt.connect(mqttHost);
client.on('connect', function () {
client.subscribe(topicName);
});
client.on('message', function (topic, message) {
context = topic + " : " + message.toString();
var date = new Date();
console.log(date.getTime() + " - " + context);
});
npm install
node publisher.js
node subscriber.js
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
38 | |
19 | |
13 | |
13 | |
11 | |
10 | |
10 | |
10 | |
8 | |
8 |