{"id":"46a02e3970b14850","repo":"nestjs/nest","slug":"not-initialized-please-call-the-listen-startal-46a02e","errorCode":null,"errorMessage":"Not initialized. Please call the \"listen\"/\"startAllMicroservices\" method before accessing the server.","messagePattern":"Not initialized\\. Please call the \"listen\"/\"startAllMicroservices\" method before accessing the server\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/microservices/server/server-mqtt.ts","lineNumber":296,"sourceCode":"      this._status$.next(MqttStatus.DISCONNECTED);\n    });\n  }\n\n  public registerCloseListener(client: MqttClient) {\n    client.on(MqttEventsMap.CLOSE, () => {\n      this._status$.next(MqttStatus.CLOSED);\n    });\n  }\n\n  public registerConnectListener(client: MqttClient) {\n    client.on(MqttEventsMap.CONNECT, () => {\n      this._status$.next(MqttStatus.CONNECTED);\n    });\n  }\n\n  public unwrap<T>(): T {\n    if (!this.mqttClient) {\n      throw new Error(\n        'Not initialized. Please call the \"listen\"/\"startAllMicroservices\" method before accessing the server.',\n      );\n    }\n    return this.mqttClient as T;\n  }\n\n  public on<\n    EventKey extends keyof MqttEvents = keyof MqttEvents,\n    EventCallback extends MqttEvents[EventKey] = MqttEvents[EventKey],\n  >(event: EventKey, callback: EventCallback) {\n    if (this.mqttClient) {\n      this.mqttClient.on(event, callback as any);\n    } else {\n      this.pendingEventListeners.push({ event, callback });\n    }\n  }\n\n  protected initializeSerializer(options: MqttOptions['options']) {","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/packages/microservices/server/server-mqtt.ts#L278-L314","documentation":"Error \"Not initialized. Please call the \"listen\"/\"startAllMicroservices\" method before accessing the server.\" thrown in nestjs/nest.","triggerScenarios":"Thrown at packages/microservices/server/server-mqtt.ts:296 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Call app.listen() or app.startAllMicroservices() before accessing the MQTT server instance.","Await listen() so the MQTT connection is established before use.","Move any code accessing the server to run after bootstrap listening completes."],"exampleFix":"const app = await NestFactory.createMicroservice(AppModule, opts);\nawait app.listen();\nconst server = app.get(ServerMqtt);","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}