Skip to content

Commit

Permalink
Added a preUnsubscribe handler, analog to authorizeSubscribe
Browse files Browse the repository at this point in the history
Invoked then a client unsubscribes from topics. Just like authorizeSubscribe, this function allows modifying the topics because executing the unsubscribe. This is needed in cases where topics are modified in authorizeSubscribe. For example:

```js
aedes.authorizeSubscribe = (client, subscription, callback) => {
  // overwrite subscription: force client to its namespace
  subscription.topic = `/${client.id}/${subscription.topic}`;
  callback(null, subscription);
}

aedes.preUnsubscribe = (client, packet) => {
  // overwrite unsubscriptions: force client to its namespace
  for (let i in packet.unsubscriptions) {
    packet.unsubscriptions[i] = `/${client.id}/${packet.unsubscriptions[i]}`;
  }
}
```

Includes fix-ups:
- Update docs/Aedes.md
  Co-authored-by: Daniel Lando <daniel.sorridi@gmail.com>
- added defaultPreUnsubscribe
  • Loading branch information
chfritz committed Apr 1, 2021
1 parent 126fd4d commit 1436c79
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const defaultOptions = {
authorizePublish: defaultAuthorizePublish,
authorizeSubscribe: defaultAuthorizeSubscribe,
authorizeForward: defaultAuthorizeForward,
preUnsubscribe: defaultPreUnsubscribe,
published: defaultPublished,
trustProxy: false,
trustedProxies: [],
Expand Down Expand Up @@ -69,6 +70,7 @@ function Aedes (opts) {
this.authorizePublish = opts.authorizePublish
this.authorizeSubscribe = opts.authorizeSubscribe
this.authorizeForward = opts.authorizeForward
this.preUnsubscribe = opts.preUnsubscribe
this.published = opts.published

this.decodeProtocol = opts.decodeProtocol
Expand Down Expand Up @@ -337,6 +339,8 @@ function defaultAuthorizeForward (client, packet) {
return packet
}

function defaultPreUnsubscribe (client, packet) { }

function defaultPublished (packet, client, callback) {
callback(null)
}
Expand Down
24 changes: 24 additions & 0 deletions docs/Aedes.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [Handler: authorizePublish (client, packet, callback)](#handler-authorizepublish-client-packet-callback)
- [Handler: authorizeSubscribe (client, subscription, callback)](#handler-authorizesubscribe-client-subscription-callback)
- [Handler: authorizeForward (client, packet)](#handler-authorizeforward-client-packet)
- [Handler: preUnsubscribe (client, packet)](#handler-preunsubscribe-client-packet)
- [Handler: published (packet, client, callback)](#handler-published-packet-client-callback)

## new Aedes([options]) / new Aedes.Server([options])
Expand Down Expand Up @@ -386,6 +387,28 @@ aedes.authorizeForward = function (client, packet) {
}
```
## Handler: preUnsubscribe (client, packet)
- client: [`<Client>`](./Client.md)
- packet: `<aedes-packet>` & [`UNSUBSCRIBE`][UNSUBSCRIBE]
Invoked when a client unsubscribes from topics. Just like `authorizeSubscribe`, this function allows modifying the topics before executing the unsubscribe. This may be needed in cases where topics were modified in authorizeSubscribe. For example:
```js
aedes.authorizeSubscribe = (client, subscription, callback) => {
// overwrite subscription: force client to its namespace
subscription.topic = `/${client.id}/${subscription.topic}`;
callback(null, subscription);
}

aedes.preUnsubscribe = (client, packet) => {
// overwrite unsubscriptions: force client to its namespace
for (let i in packet.unsubscriptions) {
packet.unsubscriptions[i] = `/${client.id}/${packet.unsubscriptions[i]}`;
}
}
```
## Handler: published (packet, client, callback)
- packet: `<aedes-packet>` & [`PUBLISH`][PUBLISH]
Expand All @@ -397,6 +420,7 @@ same as [`Event: publish`](#event-publish), but provides a backpressure function
[CONNECT]: https://github.com/mqttjs/mqtt-packet#connect
[CONNACK]: https://github.com/mqttjs/mqtt-packet#connack
[SUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#subscribe
[UNSUBSCRIBE]: https://github.com/mqttjs/mqtt-packet#unsubscribe
[PINGREQ]: https://github.com/mqttjs/mqtt-packet#pingreq
[PUBLISH]: https://github.com/mqttjs/mqtt-packet#publish
[PUBREL]: https://github.com/mqttjs/mqtt-packet#pubrel
4 changes: 4 additions & 0 deletions lib/handlers/unsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ function UnsubscribeState (client, packet, finish) {

function handleUnsubscribe (client, packet, done) {
const broker = client.broker
if (broker.preUnsubscribe) {
broker.preUnsubscribe(client, packet)
}

const unsubscriptions = packet.unsubscriptions
let err

Expand Down

0 comments on commit 1436c79

Please sign in to comment.