Created
August 9, 2015 17:48
-
-
Save cefn/2e87416aa24bfdddbb7d to your computer and use it in GitHub Desktop.
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 characters
| var mosca = require("mosca"); | |
| var mqtt = require("mqtt"); | |
| function promiseMosca(){ | |
| var moscaOpts = { | |
| http:{ | |
| port:3000, | |
| static:false, | |
| bundle:false | |
| }, | |
| onlyHttp:true | |
| }; | |
| return new Promise(function(resolve, reject){ | |
| var mqttServer = new mosca.Server(moscaOpts); | |
| mqttServer.on("ready", function(){ | |
| resolve(mqttServer); | |
| }); | |
| }); | |
| } | |
| function promiseMosquitto(){ | |
| return Promise.resolve(); | |
| } | |
| function promiseClient(){ | |
| return new Promise(function(resolve, reject){ | |
| var mqttClient = mqtt.connect("ws://localhost:3000"); | |
| mqttClient.on("connect", function(){ | |
| resolve(mqttClient); | |
| }); | |
| }); | |
| } | |
| function promiseReceipt(client){ | |
| return new Promise(function(resolve, reject){ | |
| client.on("message", function(topic, message, packet){ | |
| resolve([topic, message.toString()]); | |
| }); | |
| }); | |
| } | |
| function promiseSubscription(client, topic){ | |
| return new Promise(function(resolve, reject){ | |
| client.subscribe(topic, {qos:1}, function(err, granted){ | |
| var result = Array.prototype.slice.call(arguments); | |
| resolve(result); | |
| }); | |
| }); | |
| } | |
| function promisePublication(client,topic,message){ | |
| return new Promise(function(resolve, reject){ | |
| client.publish(topic, message, {qos:1, retain:true}, function(){ | |
| resolve(Array.prototype.slice.call(arguments)); | |
| }); | |
| }); | |
| } | |
| var testTopic = "Hello", | |
| testMessage = "World"; | |
| //var serverPromise = promiseMosca(); | |
| var serverPromise = promiseMosquitto(); | |
| var testPromise = serverPromise | |
| .then(function() { | |
| return promiseClient() | |
| }) | |
| .then(function(receiver) { | |
| var receivePromise = promiseReceipt(receiver); //initialised before publication so no events are missed | |
| return Promise.resolve() | |
| .then(function(){ | |
| return promiseClient(); | |
| }) | |
| .then(function(sender){ | |
| return promisePublication(sender,testTopic,testMessage); | |
| }) | |
| .then(function(){ //move this clause to first in the list, and Mosca delivers the message! | |
| return promiseSubscription(receiver, testTopic); | |
| }) | |
| .then(function(){ | |
| return receivePromise; //promise initialised earlier is passed back | |
| }); | |
| }); | |
| testPromise.then(function(receiveResult){ | |
| console.log("Message Received: " + JSON.stringify(receiveResult)); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for taking the time to look at this!. Providing a configuration like the one below indeed resolves the issue altogether.
Although I previously had a persistence configuration (and removed it to make a more minimal test case) there must have been something wrong with the configuration I provided. This one is minimal but works...