Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express')
- const path = require('path')
- const PORT = process.env.PORT || 5000
- express()
- .use(express.static(path.join(__dirname, 'public')))
- .set('views', path.join(__dirname, 'views'))
- .set('view engine', 'ejs')
- .get('/', (req, res) => res.render('pages/index'))
- .listen(PORT, () => console.log(`Listening on ${ PORT }`))
- var mqtt = require('mqtt') //, url = require('url');
- // Parse
- // var mqtt_url = url.parse(process.env.CLOUDMQTT_URL || 'mqtt://localhost:1883');
- //var auth = (mqtt_url.auth || ':').split(':');
- // Create a client connection
- //var client = mqtt.createClient(mqtt_url.port, mqtt_url.hostname, {
- // username: auth[0],
- // password: auth[1]
- //});
- var client = mqtt.connect(process.env.CLOUDMQTT_URL+'/?clientId='+ new Date().getTime())
- client.on('connect', function() { // When connected
- // subscribe to a topic
- client.subscribe('hello', function() {
- // when a message arrives, do something with it
- client.on('message', function(topic, message, packet) {
- console.log("Received '" + message + "' on '" + topic + "'");
- });
- });
- // publish a message to a topic
- // client.publish('hello/world', 'my message', function() {
- // console.log("Message is published");
- // client.end(); // Close the connection when published
- // });
- });
Add Comment
Please, Sign In to add comment