Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfeidau committed Sep 7, 2013
0 parents commit 734fa0a
Show file tree
Hide file tree
Showing 11 changed files with 290 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
.idea
node_modules
build
npm-debug.log
.DS_Store
*.iml
16 changes: 16 additions & 0 deletions .jshintrc
@@ -0,0 +1,16 @@
{
"node": true,
"strict": true,
"laxcomma": true,
"curly": false,
"laxbreak": true,
"expr": true,
"predef": [
"describe",
"it",
"before",
"beforeEach",
"after",
"afterEach"
]
}
22 changes: 22 additions & 0 deletions LICENSE-MIT
@@ -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.
19 changes: 19 additions & 0 deletions Makefile
@@ -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
41 changes: 41 additions & 0 deletions README.md
@@ -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.
17 changes: 17 additions & 0 deletions examples/subscriptions.js
@@ -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!');
7 changes: 7 additions & 0 deletions index.js
@@ -0,0 +1,7 @@
'use strict';

var Router = require('./lib/mqtt-router.js');

exports.wrap = function(mqttclient){
return new Router(mqttclient);
};
20 changes: 20 additions & 0 deletions lib/endpoint.js
@@ -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;
68 changes: 68 additions & 0 deletions lib/mqtt-router.js
@@ -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;
33 changes: 33 additions & 0 deletions package.json
@@ -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"
}
}
41 changes: 41 additions & 0 deletions test/router.js
@@ -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!');


});
});

0 comments on commit 734fa0a

Please sign in to comment.