nest-mqtt icon indicating copy to clipboard operation
nest-mqtt copied to clipboard

Multiple `MqttModule.forRoot()` global instances

Open tukusejssirs opened this issue 3 years ago • 2 comments

In my use case, I need two global MQTT clients: one using MQTT protocol, the other using websockets.

I added the following into app.module.ts:

@Module({
  imports: [
    MqttModule.forRoot({
      port: 1883,
      logger: {
        useValue: new Logger('MqttClient')
      }
    }),
    MqttModule.forRoot({
      protocol: 'ws',
      port: 4000,
      logger: {
        useValue: new Logger('MqttClientWs')
      }
    })
  ],
})

Now, in the service (@Inject(MqttService) private readonly mqttService: MqttService), how can I differentiate between them?

tukusejssirs avatar Jan 06 '22 11:01 tukusejssirs

I think this SO answer could be implemented to add some token to create separate MqttService instances.

However, I am quite new to Nest to fully understand it. As I see it, we could add aditional, optional token property to MqttModuleOptions and MqttModuleAsyncOptions, right?

tukusejssirs avatar Jan 06 '22 12:01 tukusejssirs

NestJS use string token as the identifier of instance container(In NestJS v8 using object reference for Class Provider).

nest-mqtt using a constant key as mqtt instance token and can't extend anymore.

Add multiple instance support is easy, but this lib should add multiple client getter, operator and event identifier at the same time what may cause a breaking change.

microud avatar Jan 07 '22 00:01 microud