aedes icon indicating copy to clipboard operation
aedes copied to clipboard

feat: preUnsubscribe handler

Open chfritz opened this issue 3 years ago • 11 comments

Invoked then a client unsubscribes from topics. Just like authorizeSubscribe, this function allows modifying the topics before executing the unsubscribe. This is needed in cases where topics are modified in authorizeSubscribe. For example:

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, callback) => {
  // overwrite unsubscriptions: force client to its namespace
  for (let i in packet.unsubscriptions) {
    packet.unsubscriptions[i] = `/${client.id}/${packet.unsubscriptions[i]}`;
  }
  callback(client, packet)
}

chfritz avatar Mar 31 '21 17:03 chfritz

Pull Request Test Coverage Report for Build 709159642

  • 5 of 5 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 99.555%

Totals Coverage Status
Change from base Build 689232499: 0.002%
Covered Lines: 804
Relevant Lines: 805

💛 - Coveralls

coveralls avatar Mar 31 '21 19:03 coveralls

The main question is what's the use case of modifying subscription topic in authorizeSubscribe ?

gnought avatar Apr 01 '21 15:04 gnought

@gnought You mean preSubscribe ?

robertsLando avatar Apr 01 '21 15:04 robertsLando

@gnought the example I'm providing in the description is the use-case I need it for: I need to force clients into their own namespace, without the clients needing to know about their namespaces. Clients may subscribe to #, i.e., get everything, but will actually be reduced to /${client.id}/#, i.e., everything in their namespace only.

chfritz avatar Apr 01 '21 16:04 chfritz

@robertsLando I've made preUnsubscribe async as you suggested.

chfritz avatar Apr 01 '21 16:04 chfritz

I believe you also hook authorizePublish to modify the PUBLISH topic, am I right?

gnought avatar Apr 01 '21 16:04 gnought

yes, exactly, @gnought .

chfritz avatar Apr 01 '21 16:04 chfritz

How about if we add the support of client isolation? Will it fit in your case? @chfritz

gnought avatar Apr 03 '21 08:04 gnought

@mcollina : will do. @gnought : maybe. What do you mean by support for client isolation?

chfritz avatar Apr 03 '21 14:04 chfritz

Just like your use case, we prepend some client-specific string in client topics.

gnought avatar Apr 03 '21 19:04 gnought

I see. Yes, I think that would work for me.

chfritz avatar Apr 03 '21 20:04 chfritz