rogerin

Untitled

Dec 1st, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mosca = require('mosca')
  2.  
  3. var settings = {
  4.   port: 1885
  5. };
  6.  
  7. //here we start mosca
  8. var server = new mosca.Server(settings);
  9. server.on('ready', setup);
  10.  
  11. // fired when the mqtt server is ready
  12. function setup() {
  13.   console.log('Mosca server is up and running')
  14. }
  15.  
  16. // fired whena  client is connected
  17. server.on('clientConnected', function(client) {
  18.   console.log('client connected', client.id);
  19. });
  20.  
  21. // fired when a message is received
  22. server.on('published', function(packet, client) {
  23.   console.log('Published : ', packet.payload.toString());
  24.  
  25.  
  26.     var b = new Buffer(packet.payload);
  27.     console.log('str' + b);
  28.  
  29.  
  30.     console.log('str' + b.toString());
  31. });
  32.  
  33. // fired when a client subscribes to a topic
  34. server.on('subscribed', function(topic, client) {
  35.   console.log('subscribed : ', topic);
  36. });
  37.  
  38. // fired when a client subscribes to a topic
  39. server.on('unsubscribed', function(topic, client) {
  40.   console.log('unsubscribed : ', topic);
  41. });
  42.  
  43. // fired when a client is disconnecting
  44. server.on('clientDisconnecting', function(client) {
  45.   console.log('clientDisconnecting : ', client.id);
  46. });
  47.  
  48. // fired when a client is disconnected
  49. server.on('clientDisconnected', function(client) {
  50.   console.log('clientDisconnected : ', client.id);
  51. });
Add Comment
Please, Sign In to add comment