Skip to content

Commit

Permalink
Connect-Connack roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Jan 11, 2015
0 parents commit 5d5e57b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript
39 changes: 39 additions & 0 deletions aedes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

var mqemitter = require('mqemitter')
, mqtt = require('mqtt-connection')
, parseStream = mqtt.parseStream
, generateStream = mqtt.generateStream
, through = require('through2')
, EE = require('events').EventEmitter

module.exports = aedes

function aedes() {

var broker = mqemitter()
, ee = new EE()

ee.broker = broker
ee.handle = handle

return ee

function handle(conn) {
var inStream = conn.pipe(parseStream())
, outStream = generateStream()
, client = through.obj(process)

client.out = outStream

outStream.pipe(conn)

inStream.pipe(client)
}

function process(packet, enc, done) {
this.out.write({
cmd: 'connack'
, returnCode: 0
}, done)
}
}
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "aedes",
"version": "0.0.1",
"description": "Stream-based MQTT broker",
"main": "aedes.js",
"scripts": {
"test": "tape test.js | faucet"
},
"repository": {
"type": "git",
"url": "[email protected]:mcollina/aedes.git"
},
"keywords": [
"mqtt",
"broker",
"stream",
"internet",
"of",
"things"
],
"author": "Matteo Collina <[email protected]>",
"license": "MIT",
"devDependencies": {
"faucet": "0.0.1",
"tape": "^3.0.3",
"through2": "^0.6.3",
"reduplexer": "^1.1.0"
},
"dependencies": {
"mqemitter": "^0.2.1",
"mqstreams": "^0.1.0",
"mqtt": "^1.0.0-pre.6",
"mqtt-connection": "^2.1.0"
}
}
38 changes: 38 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

var test = require('tape').test
, mqtt = require('mqtt-connection')
, parseStream = mqtt.parseStream
, generateStream = mqtt.generateStream
, through = require('through2')
, reduplexer = require('reduplexer')
, aedes = require('./')

test('connect and connack (minimal)', function(t) {
var broker = aedes()
, inStream = generateStream()
, outStream = parseStream()
, conn = reduplexer(outStream, inStream)

inStream.write({
cmd: 'connect'
, protocolId: 'MQTT'
, protocolVersion: 4
, clean: true
, clientId: 'my-client'
, keepalive: 0
})

outStream.on('data', function(packet) {
t.deepEqual(packet, {
cmd: 'connack'
, returnCode: 0
, length: 2
, qos: 0
, retain: false
, dup: false
}, 'successful connack')
t.end()
})

broker.handle(conn)
})

0 comments on commit 5d5e57b

Please sign in to comment.