Guest User

Untitled

a guest
Dec 22nd, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. const express = require('express')
  2. const path = require('path')
  3. const PORT = process.env.PORT || 5000
  4.  
  5. express()
  6. .use(express.static(path.join(__dirname, 'public')))
  7. .set('views', path.join(__dirname, 'views'))
  8. .set('view engine', 'ejs')
  9. .get('/', (req, res) => res.render('pages/index'))
  10. .listen(PORT, () => console.log(`Listening on ${ PORT }`))
  11.  
  12.  
  13.  
  14. var mqtt = require('mqtt') //, url = require('url');
  15. // Parse
  16. // var mqtt_url = url.parse(process.env.CLOUDMQTT_URL || 'mqtt://localhost:1883');
  17. //var auth = (mqtt_url.auth || ':').split(':');
  18.  
  19. // Create a client connection
  20. //var client = mqtt.createClient(mqtt_url.port, mqtt_url.hostname, {
  21. // username: auth[0],
  22. // password: auth[1]
  23. //});
  24.  
  25. var client = mqtt.connect(process.env.CLOUDMQTT_URL+'/?clientId='+ new Date().getTime())
  26.  
  27.  
  28. client.on('connect', function() { // When connected
  29.  
  30. // subscribe to a topic
  31. client.subscribe('hello', function() {
  32. // when a message arrives, do something with it
  33. client.on('message', function(topic, message, packet) {
  34. console.log("Received '" + message + "' on '" + topic + "'");
  35. });
  36. });
  37.  
  38. // publish a message to a topic
  39. // client.publish('hello/world', 'my message', function() {
  40. // console.log("Message is published");
  41. // client.end(); // Close the connection when published
  42. // });
  43. });
Add Comment
Please, Sign In to add comment