MQTT

The MQTT is a lightweight messaging protocol, optimized for high-latency.

Installation

Before we start, we have to install required package:

  1. $ npm i --save mqtt

Overview

In order to switch to MQTT transporter, we need to modify an options object passed to the createMicroservice() method.

  1. @@filename(main)
  2. const app = await NestFactory.createMicroservice(ApplicationModule, {
  3. transport: Transport.MQTT,
  4. options: {
  5. host: 'localhost',
  6. port: 1883,
  7. },
  8. });

info Hint Transport enumerator is imported from the @nestjs/microservices package.

Options

There are a bunch of available options that determine a transporter behavior. They are well-described here.