[ ] Regression
[X] Bug report
[ ] Feature request
[X] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Endless connecting without exception or success to microservice.
Exception or successful connection.
main.ts
import { INestMicroservice } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
import { Transport } from "@nestjs/microservices";
import { AppModule } from "./app.module";
async function bootstrap(): Promise<void> {
const app: INestMicroservice = await NestFactory.createMicroservice(
AppModule,
{
transport: Transport.MQTT,
options: {
host: "broker.hivemq.com",
port: 1883
}
}
);
await app.listenAsync();
}
bootstrap();
app.module.ts
import { Module } from "@nestjs/common";
import { AppService } from "./app.service";
@Module({
providers: [AppService]
})
export class AppModule {}
app.service.ts
import { Injectable, Logger } from "@nestjs/common";
import { Client, ClientProxy, Transport } from "@nestjs/microservices";
import { Observable } from "rxjs";
@Injectable()
export class AppService {
@Client({ transport: Transport.MQTT })
public client: ClientProxy;
public async onModuleInit(): Promise<void> {
Logger.log("Connecting");
await this.client.connect();
Logger.log("Connected");
}
public sendMessage(): Observable<number> {
const pattern: {} = { cmd: "sum" };
const data: number[] = [5, 6];
return this.client.send<number>(pattern, data);
}
}
Nest version: 5.3.0
For Tooling issues:
- Node version: 10.9.0
- Platform: Mac OS
Others:
You can use the broker.hivemq.com which is publicly accessible and actually works.
This issue doesn't appear with normally instantiated micro service (instead of broker.hivemq.com).
So just the MQTT transport is affected?
MQTT transport works properly as well. Not sure it cannot connect only to broker.hivemq.com
Alright, thanks I will try some other MQTT brokers tomorrow and I'll leave feedback here.
@kamilmysliwiec I tried the above code on another MQTT broker (which works fine as well with MQTT Box), but the Promise for connect() never resolves (I expected "Connected" to be logged):
Debugger listening on ws://127.0.0.1:49214/e5eb44e6-23dd-4b99-a1a9-63375c4805ff
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
[Nest] 20435 - 9/4/2018, 12:23:33 PM [NestFactory] Starting Nest application...
[Nest] 20435 - 9/4/2018, 12:23:33 PM [InstanceLoader] AppModule dependencies initialized +4ms
[Nest] 20435 - 9/4/2018, 12:23:33 PM Connecting +122ms
[Nest] 20435 - 9/4/2018, 12:23:33 PM [NestMicroservice] Nest microservice successfully started +24ms
I pushed some fixes to the MQTT client. Indeed, it had some issues with error handling. Anyway, a valid connection is still handled properly. Please, update your packages and check once again.
@kamilmysliwiec
options: {
hostname: 'broker.hivemq.com',
port: 1883,
protocol: 'mqtt',
}
@kamilmysliwiec I believe I missunderstood the documentation and therefore I used simply wrong connection details. Maybe you can clarify the difference between the Nest microservice which connects to a MQTT broker and a ClientProxy which can also be connected to a microservice. My current understanding is that the Nest microservice connection will be used for listening on all messagepatterns in my controllers. The ClientProxy again can be a completely different transport which I could (additionally) send messages to. Is that correct?
However I identified an issue:
@Client({
transport: Transport.MQTT,
options: { host: "broker.hivemq.com", port: 1883 }
})
it will always connect to 127.0.0.1:1883
When I use hostname instead of host with the exact same values it does connect just fine. The MQTT library code however says:
export interface IClientOptions extends ISecureClientOptions {
port?: number // port is made into a number subsequently
host?: string // host does NOT include port
hostname?: string
One last question:
When Nest logs:
[Nest] 35342 - 9/7/2018, 10:28:12 AM [NestMicroservice] Nest microservice successfully started +27ms
does this mean it is connected to the broker specified in the createMicroservice options?
Thank you for all your answers!
My current understanding is that the Nest microservice connection will be used for listening on all message patterns in my controllers. The ClientProxy again can be a completely different transport which I could (additionally) send messages to. Is that correct?
Exactly.
it will always connect to 127.0.0.1:1883. When I use hostname instead of host with the exact same values it does connect just fine. The MQTT library code however says:
As I mentioned, this interface is an external one, it comes directly from MQTT library https://github.com/mqttjs/MQTT.js/blob/master/types/lib/client-options.d.ts. We simply pass down all options properties to the MqttClient instance.
does this mean it is connected to the broker specified in the createMicroservice options?
Yes
Hi there, just want to mention that I had trouble to connect to my MQTT broker (set up on the side of the Nest app, via docker-compose) and finally made it work by using hostname instead of host in the Nest Microservice connection options, as mentioned above.
Perhaps something needs to be updated in the documentation?
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.