Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 734fa0a
Showing
11 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.idea | ||
node_modules | ||
build | ||
npm-debug.log | ||
.DS_Store | ||
*.iml |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"node": true, | ||
"strict": true, | ||
"laxcomma": true, | ||
"curly": false, | ||
"laxbreak": true, | ||
"expr": true, | ||
"predef": [ | ||
"describe", | ||
"it", | ||
"before", | ||
"beforeEach", | ||
"after", | ||
"afterEach" | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2012 Mark Wolfe | ||
|
||
Permission is hereby granted, free of charge, to any person | ||
obtaining a copy of this software and associated documentation | ||
files (the "Software"), to deal in the Software without | ||
restriction, including without limitation the rights to use, | ||
copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
REPORTER = spec | ||
|
||
all: jshint test | ||
|
||
test: | ||
@NODE_ENV=test ./node_modules/.bin/mocha --recursive --reporter $(REPORTER) --timeout 3000 | ||
|
||
jshint: | ||
jshint lib examples test index.js | ||
|
||
tests: test | ||
|
||
tap: | ||
@NODE_ENV=test ./node_modules/.bin/mocha -R tap > results.tap | ||
|
||
unit: | ||
@NODE_ENV=test ./node_modules/.bin/mocha --recursive -R xunit > results.xml --timeout 3000 | ||
|
||
.PHONY: test tap unit jshint |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# mqtt-router [![Build Status](https://travis-ci.org/wolfeidau/mqtt-router.png?branch=master)](https://travis-ci.org/wolfeidau/mqtt-router) | ||
|
||
This module a router for use with MQTT subscriptions. | ||
|
||
[![NPM](https://nodei.co/npm/mqtt-router.png)](https://nodei.co/npm/mqtt-router/) | ||
[![NPM](https://nodei.co/npm-dl/mqtt-router.png)](https://nodei.co/npm/mqtt-router/) | ||
|
||
## Installation | ||
|
||
``` | ||
npm install mqtt-router | ||
``` | ||
|
||
# usage | ||
|
||
Exposes an array of functions which retrieves and returns data. | ||
|
||
```javascript | ||
var mqtt = require('mqtt'); | ||
var mqttrouter = require('mqtt-router'); | ||
var debug = require('debug')('mqtt:consumer'); | ||
|
||
var client = mqtt.createClient(); | ||
|
||
var router = mqttrouter.wrap(client); | ||
|
||
router.subscribe('$RPC/time/request', function(topic, message){ | ||
debug('received', topic, message); | ||
}); | ||
``` | ||
|
||
*NOTE:* This currently just does simple subscriptions without wildcards, this is something I will work on next. | ||
|
||
# TODO | ||
|
||
* Work on wild cards using $ and # in the subscription. | ||
* Look at using more of [houkou](https://github.com/deoxxa/houkou) to break up the the topic into params enabling more generic handlers. | ||
|
||
## License | ||
Copyright (c) 2013 Mark Wolfe | ||
Licensed under the MIT license. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
var mqtt = require('mqtt'); | ||
var mqttrouter = require('../index.js'); | ||
|
||
var client = mqtt.createClient(); | ||
var router = mqttrouter.wrap(client); | ||
|
||
router.subscribe('$TEST/dev/request', function(topic, message){ | ||
console.log('request handler', topic, message); | ||
}); | ||
|
||
router.subscribe('$TEST/dev/reply', function(topic, message){ | ||
console.log('reply handler', topic, message); | ||
}); | ||
|
||
client.publish('$TEST/dev/reply', 'hello me!'); | ||
client.publish('$TEST/dev/request', 'hello me!'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
var Router = require('./lib/mqtt-router.js'); | ||
|
||
exports.wrap = function(mqttclient){ | ||
return new Router(mqttclient); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
/* | ||
* mqtt-router | ||
* https://github.com/wolfeidau/mqtt-router | ||
* | ||
* Copyright (c) 2013 Mark Wolfe | ||
* Licensed under the MIT license. | ||
*/ | ||
var Houkou = require('houkou'); | ||
var log = require('debug')('mqtt-router:endpoint'); | ||
|
||
var Endpoint = function(topic, handler){ | ||
log('endpoint', topic, handler); | ||
this.topic = topic; | ||
this.route = new Houkou(topic); | ||
this.handlers = [handler]; | ||
|
||
}; | ||
|
||
module.exports = Endpoint; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
/* | ||
* mqtt-router | ||
* https://github.com/wolfeidau/mqtt-router | ||
* | ||
* Copyright (c) 2013 Mark Wolfe | ||
* Licensed under the MIT license. | ||
*/ | ||
var mqtt = require('mqtt'); | ||
var Endpoint = require('./endpoint.js'); | ||
var log = require('debug')('mqtt-router:router'); | ||
|
||
var Router = function (mqttclient) { | ||
|
||
this.mqttclient = mqttclient || mqtt.createClient(); | ||
|
||
this.endpoints = []; | ||
|
||
var self = this; | ||
|
||
this.mqttclient.on('message', function(topic, message){ | ||
log('message', topic, message); | ||
|
||
self.endpoints.forEach(function(endpoint){ | ||
log('endpoint', endpoint.topic); | ||
if(endpoint.route.match(topic)){ | ||
endpoint.handlers.forEach(function(handler){ | ||
handler(topic, message); | ||
}); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
this.subscribe = function(topic, handler){ | ||
|
||
var safeTopic = self._escapeTopic(topic); | ||
|
||
log('endpoint', safeTopic); | ||
|
||
var endpoint = self._endpointMatches(safeTopic)[0]; | ||
|
||
if (endpoint) { | ||
endpoint.handlers.push(handler); | ||
} else { | ||
self.endpoints.push(new Endpoint(safeTopic, handler)); | ||
} | ||
log('subscribe', topic); | ||
self.mqttclient.subscribe(topic); | ||
|
||
}; | ||
|
||
this._endpointMatches = function (topic) { | ||
return self.endpoints.map(function (endpoint) { | ||
if (endpoint.topic === topic) { | ||
return endpoint; | ||
} | ||
}); | ||
}; | ||
|
||
this._escapeTopic = function(topic){ | ||
return topic.replace(/\$/, "\\$"); | ||
}; | ||
|
||
}; | ||
|
||
|
||
module.exports = Router; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "mqtt-router", | ||
"version": "0.1.0", | ||
"description": "Router for mqtt subscriptions.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wolfeidau/mqtt-router.git" | ||
}, | ||
"keywords": [ | ||
"MQTT", | ||
"router", | ||
"subscriptions" | ||
], | ||
"author": "Mark Wolfe <mark@wolfe.id.au>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/wolfeidau/mqtt-router/issues" | ||
}, | ||
"dependencies": { | ||
"mqtt": "~0.3.1", | ||
"houkou": "~0.2.1", | ||
"debug": "~0.7.2" | ||
}, | ||
"devDependencies": { | ||
"chai": "~1.7.2", | ||
"sinon": "~1.7.3", | ||
"mocha": "~1.12.1" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var chai = require('chai'); | ||
var sinon = require('sinon'); | ||
var mqtt = require('mqtt'); | ||
var log = require('debug')('test:mqtt-router'); | ||
var expect = chai.expect; | ||
var mqttrouter = require('../index.js'); | ||
|
||
describe('client', function () { | ||
|
||
it('should route one message to the handler', function (done) { | ||
|
||
var mqttclient = mqtt.createClient(); | ||
|
||
var firstTopic = 'TEST/localtime/request'; | ||
var secondTopic = 'TEST/localtime/reply'; | ||
|
||
function check(){ | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.getCall(0).args[0]).to.equal(firstTopic); | ||
done(); | ||
} | ||
|
||
var callback = sinon.spy(function(topic, message){ | ||
log('msg', topic, message); | ||
check(); | ||
}); | ||
|
||
var router = mqttrouter.wrap(mqttclient); | ||
router.subscribe(firstTopic, callback); | ||
|
||
log('publish', firstTopic); | ||
mqttclient.publish(firstTopic, 'hello firstTopic!'); | ||
|
||
log('publish', secondTopic); | ||
mqttclient.publish(secondTopic, 'hello secondTopic!'); | ||
|
||
|
||
}); | ||
}); |