Last active
April 1, 2019 13:48
-
-
Save BrainCrumbz/f71a4867a4d2c0e30ee5 to your computer and use it in GitHub Desktop.
Revisions
-
BrainCrumbz revised this gist
May 5, 2015 . 2 changed files with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,8 +17,5 @@ MQTT client received message topic: 'broker/someTopic', payload: <Buffer 7b 22 66 69 65 6c 64 41 22 3a 22 61 22 2c 22 66 69 65 6c 64 42 22 3a 22 62 22 7d>, messageId: 1 } * payload (unpacked) {"fieldA":"a","fieldB":"b"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,5 @@ client.on('message', function (topic, message, packet) { console.log(' * message', message); console.log(' * message (stringified)', JSON.stringify(message)); console.log(' * packet', packet); console.log(' * payload (unpacked)', packet.payload.toString('utf8')); }); -
BrainCrumbz revised this gist
May 5, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,6 +65,6 @@ function publishMessage() { console.log('MQTT broker sending message to board ..\n'); broker.publish(packet, function() { console.log('MQTT broker message sent'); }); } -
BrainCrumbz revised this gist
May 5, 2015 . 3 changed files with 42 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ MQTT Client is starting... MQTT client connected MQTT client subscribing to broker/someTopic... subscribe granted: [ { topic: 'broker/someTopic', qos: 1 } ] ######################################### MQTT client received message * topic broker/someTopic * message <Buffer 7b 22 66 69 65 6c 64 41 22 3a 22 61 22 2c 22 66 69 65 6c 64 42 22 3a 22 62 22 7d> * message (stringified) [123,34,102,105,101,108,100,65,34,58,34,97,34,44,34,102,105,101,108,100,66,34,58,34,98,34,125] * packet { cmd: 'publish', retain: false, qos: 1, dup: false, length: 47, topic: 'broker/someTopic', payload: <Buffer 7b 22 66 69 65 6c 64 41 22 3a 22 61 22 2c 22 66 69 65 6c 64 42 22 3a 22 62 22 7d>, messageId: 1 } * packet (stringified) {"cmd":"publish","retain":false,"qos":1,"dup":false,"length":47,"topic":"broker/someTopic","payload":[123,34,102,105,101,108,100,65,34,58,34,97,34,44,34,102 ,105,101,108,100,66,34,58,34,98,34,125],"messageId":1} * payload <Buffer 7b 22 66 69 65 6c 64 41 22 3a 22 61 22 2c 22 66 69 65 6c 64 42 22 3a 22 62 22 7d> * payload (unpacked) {"fieldA":"a","fieldB":"b"}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,3 +3,5 @@ Some instructions: * Create two separate directories, one for the client, one for the server * Copy client related files in its directory. Do the same for server files * Rename client-package.json as just package.json. Do the same for server-package.json At the moment, for some reason, server message 'MQTT broker is up and running' is logged twice, as if onCreated is run twice. 'ready' event, instead, runs only once. This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ MQTT broker is starting... MQTT broker is up and running MQTT broker is up and running MQTT broker is ready ######################################### MQTT broker sending message to board .. MQTT broker detected a published message * packet: { topic: 'broker/someTopic', payload: '{"fieldA":"a","fieldB":"b"}', qos: 1, retain: false } * packet payload: {"fieldA":"a","fieldB":"b"} MQTT broker message sent -
BrainCrumbz created this gist
May 5, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ { "name": "mosca-publish-demo-client", "version": "1.0.0", "description": "", "main": "client.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "BrainCrumbz", "license": "MIT", "dependencies": { "mqtt": "^1.1.3" } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ var mqtt = require('mqtt'); console.log('MQTT Client is starting...'); /* // remote Test Mosquitto MQTT server instance var client = mqtt.connect('mqtt://test.mosquitto.org'); */ // local mosca MQTT server instance var client = mqtt.connect({ port: 1883, host: 'localhost', keepalive: 10000, }); var topicOfInterest = 'broker/someTopic'; var topicSubscribeOpts = { qos: 1, }; client.on('connect', function () { console.log('MQTT client connected'); console.log('MQTT client subscribing to ' + topicOfInterest + '...'); client.subscribe(topicOfInterest, topicSubscribeOpts, function onSubscribe(err, granted) { if (err) { console.log('subscribe errors:', err); } if (granted) { console.log('subscribe granted:', granted); } }); }); // fired when a message is received on one of the subscribed topic client.on('message', function (topic, message, packet) { console.log('\n\n#########################################'); console.log('MQTT client received message'); console.log(' * topic', topic); console.log(' * message', message); console.log(' * message (stringified)', JSON.stringify(message)); console.log(' * packet', packet); console.log(' * packet (stringified)', JSON.stringify(packet)); console.log(' * payload', packet.payload); console.log(' * payload (unpacked)', packet.payload.toString('utf8')); }); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ Some instructions: * Create two separate directories, one for the client, one for the server * Copy client related files in its directory. Do the same for server files * Rename client-package.json as just package.json. Do the same for server-package.json This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ { "name": "mosca-publish-demo-server", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "BrainCrumbz", "license": "MIT", "dependencies": { "mosca": "^0.29.0" }, "devDependencies": {} } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,70 @@ var mosca = require('mosca'); console.log('MQTT broker is starting...'); var inMemoryBrokerSettings = { port: 1883, // mosca (mqtt) port persistence: mosca.persistence.Memory, // using ascoltatore over memory }; var brokerSettings = inMemoryBrokerSettings; // here MQTT broker is started var broker = new mosca.Server(brokerSettings, function onCreated(err, broker) { // assume no errors console.log('MQTT broker is up and running'); }); broker.on('ready', function onReady() { console.log('MQTT broker is ready') setInterval(publishMessage, 5000); }); // fired when a client connects broker.on('clientConnected', function onClientConnected(client) { console.log('MQTT client connected, id', client.id); }); // fired when a client disconnects broker.on('clientDisconnected', function onClientDisconnected(client) { console.log('MQTT client disconnected, id', client.id); }); // fired when a message is published broker.on('published', function onPublished(packet, client) { console.log('MQTT broker detected a published message'); console.log(' * packet:', packet); console.log(' * packet payload:', packet.payload.toString()); }); var topicOfInterest = 'broker/someTopic'; var objectPayload = { fieldA: 'a', fieldB: 'b', }; var textPayload = JSON.stringify(objectPayload); var bufferPayload = new Buffer(textPayload, 'utf-8'); function publishMessage() { var packet = { topic: topicOfInterest, payload: textPayload, //payload: bufferPayload, qos: 1, retain: false, }; console.log('\n\n#########################################'); console.log('MQTT broker sending message to board ..\n'); broker.publish(packet, function() { console.log('MQTT broker message sent'); }); }