Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packet in published event should be identical with what client publishes #287

Merged
merged 1 commit into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ function removeSharp (sub) {

function callPublished (_, done) {
this.broker.published(this.packet, this.client, done)
this.broker.emit('publish', this.packet, this.client)
}

var publishFuncsSimple = [
Expand All @@ -229,11 +228,14 @@ Aedes.prototype.publish = function (packet, client, done) {
client = null
}
var p = new Packet(packet, this)
var publishFuncs = publishFuncsSimple
if (p.qos > 0) {
publishFuncs = publishFuncsQoS
}
this._series(new PublishState(this, client, p), publishFuncs, null, done)
var publishFuncs = p.qos > 0 ? publishFuncsQoS : publishFuncsSimple

this._series(new PublishState(this, client, p), publishFuncs, null, function (err) {
this.broker.emit('publish', packet, this.client)
if (done) {
done(err)
}
})
}

Aedes.prototype.subscribe = function (topic, func, done) {
Expand Down
35 changes: 32 additions & 3 deletions test/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ test('call published method with client', function (t) {
})
})

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

var broker = aedes()

broker.on('publish', function (packet, client) {
// for internal messages, client will be null
if (client) {
t.equal(packet.qos, 0)
t.equal(packet.topic, 'hello', 'topic matches')
t.equal(packet.payload.toString(), 'world', 'payload matches')
broker.close()
Expand All @@ -100,7 +101,35 @@ test('emit publish event with client', function (t) {
s.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world')
payload: Buffer.from('world'),
qos: 0
})
})

test('emit publish event with client - QoS 1', function (t) {
t.plan(4)

var broker = aedes()

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

var s = connect(setup(broker))

s.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: Buffer.from('world'),
qos: 1,
messageId: 42
})
})

Expand Down