Skip to content

Commit efef5cc

Browse files
RangerMauveRangerMauve
authored andcommitted
Initial implementation
1 parent b7d0a9a commit efef5cc

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
# async-mqtt
2+
23
Promise wrapper over MQTT.js
4+
5+
**IMPORANT: Make sure you handle rejections from returned promises because otherwise you might not see them**
6+
7+
## API
8+
9+
The API is the same as [MQTT.js](https://github.com/mqttjs/MQTT.js#api), except the following functions now return promises instead of taking callbacks
10+
11+
- publish
12+
- subscribe
13+
- unsubscribe
14+
- end

index.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"use strict";
2+
var mqtt = require("mqtt");
3+
var inArray = require("in-array");
4+
5+
var RegularClientPrototype = mqtt.client;
6+
7+
var ASYNC_METHODS = ["publish", "subscribe", "unsubscribe", "unsubscribe", "end"];
8+
9+
module.exports = {
10+
connect: connect,
11+
AsyncClient: AsyncClient
12+
};
13+
14+
function connect(brokerURL, opts) {
15+
var client = mqtt.connect(brokerURL, opts);
16+
17+
var asyncClient = new AsyncClient(client);
18+
19+
return asyncClient;
20+
}
21+
22+
function AsyncClient(client) {
23+
this._client = client;
24+
}
25+
26+
AsyncClient.prototype = {
27+
set handleMessage(newHandler) {
28+
this._client.handleMessage = newHandler;
29+
},
30+
get handleMessage() {
31+
return this._client.handleMessage;
32+
}
33+
};
34+
35+
for (var name in RegularClientPrototype) {
36+
if (inArray(ASYNC_METHODS, name))
37+
defineAsync(name);
38+
else definePassthrough(name);
39+
}
40+
41+
function definePassthrough(name) {
42+
AsyncClient.prototype[name] = function() {
43+
var client = this._client;
44+
return client[name].apply(client, arguments);
45+
};
46+
}
47+
48+
function defineAsync(name) {
49+
AsyncClient.prototype[name] = function asyncMethod() {
50+
var client = this._client;
51+
var args = [];
52+
var length = arguments.length;
53+
var i = 0;
54+
for (i; i < length; i++)
55+
args.push(arguments[i]);
56+
57+
return new Promise(function(resolve, reject) {
58+
args.push(makeCallback(resolve, reject));
59+
client.apply(client, args);
60+
});
61+
};
62+
}
63+
64+
function makeCallback(resolve, reject) {
65+
return function(err, data) {
66+
if (err)
67+
reject(err);
68+
else resolve(data);
69+
};
70+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "async-mqtt",
3+
"version": "1.0.0",
4+
"description": "Promise wrapper over MQTT.js",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/RangerMauve/async-mqtt.git"
12+
},
13+
"keywords": [
14+
"mqtt",
15+
"promise",
16+
"async",
17+
"publish",
18+
"subscribe"
19+
],
20+
"author": "rangermauve",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/RangerMauve/async-mqtt/issues"
24+
},
25+
"homepage": "https://github.com/RangerMauve/async-mqtt#readme",
26+
"dependencies": {
27+
"in-array": "^0.1.2",
28+
"mqtt": "^2.3.1"
29+
}
30+
}

0 commit comments

Comments
 (0)