nestjs-service-bus icon indicating copy to clipboard operation
nestjs-service-bus copied to clipboard

Prevent listening to non service bus events.

Open iam-amanxz opened this issue 6 months ago • 1 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @niur/[email protected] for the project I'm working on.

I encountered an issue where the service bus listened to my RPC events while running the Nestjs server. Due to this reason, the server didn't start since the properties you are destructuring don't exist inside RPC events. This would be the case for any other event that is not a service bus event. In that case, wouldn't it be better to check for the particular event before doing any sort of logic? For now, I have done a patch to my project that returns if the event is RPC. But ideally, the subscribe function should listen to only the service bus events.

Here is the diff that solved my problem:

diff --git a/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts b/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
index 42709b5..fb60eee 100644
--- a/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
+++ b/node_modules/@niur/nestjs-service-bus/lib/server/azure-service-bus.server.ts
@@ -46,6 +46,15 @@ export class AzureServiceBusServer
 
   async bindEvents(): Promise<void> {
     const subscribe = async (pattern: string) => {
+      const data = JSON.parse(pattern);
+
+      if ('rpc' in data) {
+        /**
+         * prevent subscribing to rpc patterns
+         */
+        return;
+      }
+
       const {
         metaOptions: {
           topic,
@@ -55,7 +64,7 @@ export class AzureServiceBusServer
           receiveMode,
           options,
         },
-      }: SbSubscriberMetadata = JSON.parse(pattern);
+      }: SbSubscriberMetadata = data;
 
       const receiver = this.sbClient.createReceiver(topic, name, {
         receiveMode,

This issue body was partially generated by patch-package.

iam-amanxz avatar Aug 22 '24 06:08 iam-amanxz