Skip to content

Commit

Permalink
Merge pull request #69 from GavinDmello/master
Browse files Browse the repository at this point in the history
presence module for aedes
  • Loading branch information
mcollina committed Oct 2, 2016
2 parents 1e74434 + 41e781e commit f801f7c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ specifying a `messageId` will send suback to the client.

Disconnects the client

-------------------------------------------------------
<a name="clientpresence"></a>
### client presence

You can subscribe on the following $SYS topics to get client presence

- `$SYS/+/new/clients` - Will inform about new clients connections
- `$SYS/+/disconnect/clients` - Will inform about client disconnections
The payload will contain the clientId of the connected/disconnected client


<a name="todo"></a>
## Todo

Expand Down
4 changes: 4 additions & 0 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ Aedes.prototype.unregisterClient = function (client) {
this.connectedClients--
delete this.clients[client.id]
this.emit('clientDisconnect', client)
this.publish({
topic: '$SYS/' + this.id + '/disconnect/clients',
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/+/disconnect/clients',
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())
})
})
8 changes: 5 additions & 3 deletions test/qos2.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ 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()
// Client is null for all server publishes
if (packet.topic.split('/')[0] !== '$SYS') {
t.ok(client, 'client must be passed to published method')
cb()
}
}

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

0 comments on commit f801f7c

Please sign in to comment.