Skip to content

Commit

Permalink
Correctly emit publish with the publishing client.
Browse files Browse the repository at this point in the history
Fixes #7.
  • Loading branch information
mcollina committed Nov 16, 2015
1 parent ca54cb7 commit 9bca79c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Events:
2. `err`
* `publish`: when a new packet is published, arguments:
1. `packet`
2. `client`
2. `client`, it will be null if the message is published using
[`publish`](#publish).
* `subscribe`: when a client sends a SUBSCRIBE, arguments:
1. `subscriptions`, as defined in the `subscriptions` property of the
[SUBSCRIBE](https://github.com/mqttjs/mqtt-packet#subscribe)
Expand Down
6 changes: 6 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ aedes.on('clientError', function (client, err) {
console.log('client error', client.id, err.message)
throw err
})

aedes.on('publish', function (packet, client) {
if (client) {
console.log('client', client.id)
}
})
4 changes: 2 additions & 2 deletions lib/handlers/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ function enqueuePublish (packet, done) {
break
case 1:
write(client, new PubAck(packet))
client.broker.publish(packet, done)
client.broker.publish(packet, client, done)
break
case 0:
client.broker.publish(packet, done)
client.broker.publish(packet, client, done)
break
default:
// nothing to do
Expand Down
45 changes: 36 additions & 9 deletions test/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,50 @@ test('call published method', function (t) {
})
})

test('emit publish event', function (t) {
t.plan(4)
test('call published method with client', function (t) {
t.plan(2)

var broker = aedes()

broker.published = function (packet, client, done) {
// for internal messages, client will be null
if (client) {
t.equal(packet.topic, 'hello', 'topic matches')
t.equal(packet.payload.toString(), 'world', 'payload matches')
broker.close()
done()
}
}

var s = connect(setup(broker))

s.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: new Buffer('world')
})
})

test('emit publish event with client', function (t) {
t.plan(2)

var broker = aedes()

broker.on('publish', function (packet, client) {
t.equal(packet.topic, 'hello', 'topic matches')
t.equal(packet.payload.toString(), 'world', 'payload matches')
t.equal(client, null, 'no client')
broker.close()
// for internal messages, client will be null
if (client) {
t.equal(packet.topic, 'hello', 'topic matches')
t.equal(packet.payload.toString(), 'world', 'payload matches')
broker.close()
}
})

broker.publish({
var s = connect(setup(broker))

s.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: new Buffer('world')
}, function (err) {
t.error(err, 'no error')
})
})

Expand Down

0 comments on commit 9bca79c

Please sign in to comment.