Skip to content

Commit

Permalink
presence module for aedes
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinDmello committed Sep 30, 2016
1 parent 1e74434 commit 77b6655
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ var publishFuncsQoS = [
callPublished
]
Aedes.prototype.publish = function (packet, client, done) {
//console.log(packet, client, done)
if (typeof client === 'function') {
done = client
client = null
Expand Down Expand Up @@ -270,6 +271,10 @@ Aedes.prototype.unregisterClient = function (client) {
this.connectedClients--
delete this.clients[client.id]
this.emit('clientDisconnect', client)
this.publish({
topic: '$SYS/' + this.id + '/new/clientDisconnect',
payload: new Buffer(client.id, 'utf8')
}, noop)
}

function closeClient (client, cb) {
Expand Down
52 changes: 52 additions & 0 deletions test/client-pub-sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,55 @@ test('subscribe a client programmatically with full packet', function (t) {
t.deepEqual(packet, expected, 'packet matches')
})
})

test('get message when client connects', function (t) {
t.plan(2)
var client1 = 'gav'
var broker = aedes()

broker.on('client', function (client) {
client.subscribe({
subscriptions: [{
topic: '$SYS/+/new/clients',
qos: 0
}]
}, function (err) {
t.error(err, 'no error')
})
})

var s1 = connect(setup(broker), { clientId: client1 })

s1.outStream.on('data', function (packet) {
t.equal(client1, packet.payload.toString())
})
})

test('get message when client disconnects', function (t) {
t.plan(2)
var client1 = 'gav'
var client2 = 'friend'
var broker = aedes()

broker.on('client', function (client) {
if (client.id === client1) {
client.subscribe({
subscriptions: [{
topic: '$SYS/+/new/clientDisconnect',
qos: 0
}]
}, function (err) {
t.error(err, 'no error')
})
} else {
client.close()
}
})

var s1 = connect(setup(broker), { clientId: client1 })
connect(setup(broker), { clientId: client2 })

s1.outStream.on('data', function (packet) {
t.equal(client2, packet.payload.toString())
})
})
7 changes: 4 additions & 3 deletions test/qos2.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ test('call published method with client with QoS 2', function (t) {
}

broker.published = function (packet, client, cb) {
t.ok(client, 'client must be passed to published method')

cb()
if (packet.topic.split('/').pop() !== 'clientDisconnect') {
t.ok(client, 'client must be passed to published method')
cb()
}
}

subscribe(t, subscriber, 'hello', 2, function () {
Expand Down

0 comments on commit 77b6655

Please sign in to comment.