question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connecting to microservice (MQTT)

See original GitHub issue

I’m submitting a…


[ ] 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.

Current behavior

Endless connecting without exception or success to microservice.

Expected behavior

Exception or successful connection.

Minimal reproduction of the problem with instructions

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);
  }
}

What is the motivation / use case for changing the behavior?

Environment


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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Sep 7, 2018

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

1reaction
kamilmysliwieccommented, Sep 3, 2018

MQTT transport works properly as well. Not sure it cannot connect only to broker.hivemq.com

Read more comments on GitHub >

github_iconTop Results From Across the Web

MQTT - Microservices - A progressive Node.js framework
This protocol provides a scalable and cost-efficient way to connect devices using a publish/subscribe model. A communication system built on MQTT consists ...
Read more >
Event-based-microservices: MQTT with Broker OR HTTP with ...
Both is definitely a possibility! Choose a broker that allows you to easily mix-and-match between HTTP (synchronous) communication, ...
Read more >
Event Driven Microservices Architecture for IoT Solutions ...
In this article, I'll discuss an event-driven microservices architecture approach for IoT using MQTT with HiveMQ MQTT Broker as the central ...
Read more >
REST and MQTT: Yin and Yang of Micro-Service APIs
This establishes the baseline state. All the services will simultaneously connect to a message broker.
Read more >
Getting Started with Node.js and MQTT - Blog - RisingStack
Next up, we'll add some code to connect to the broker. Once connected we'll create a topic (channel) that will be used to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found