MQTT

MQTT (Message Queuing Telemetry Transport) is an open source, lightweight messaging protocol, optimized for high-latency. This protocol provides a scalable and cost-efficient way to connect devices using a publish/subscribe model. A communication system built on MQTT consists of the publishing server, a broker and one or more clients. It is designed for constrained devices and low-bandwidth, high-latency or unreliable networks.

Installation

To start building MQTT-based microservices, first install the required package:

  1. $ npm i --save mqtt

Overview

To use the MQTT transporter, pass the following options object to the createMicroservice() method:

main.ts

  1. const app = await NestFactory.createMicroservice<MicroserviceOptions>(ApplicationModule, {
  2. transport: Transport.MQTT,
  3. options: {
  4. url: 'mqtt://localhost:1883',
  5. },
  6. });
  1. const app = await NestFactory.createMicroservice(ApplicationModule, {
  2. transport: Transport.MQTT,
  3. options: {
  4. url: 'mqtt://localhost:1883',
  5. },
  6. });

Hint The Transport enum is imported from the @nestjs/microservices package.

Options

The options object is specific to the chosen transporter. The MQTT transporter exposes the properties described here.

Client

Like other microservice transporters, you have several options for creating a MQTT ClientProxy instance.

One method for creating an instance is to use use the ClientsModule. To create a client instance with the ClientsModule, import it and use the register() method to pass an options object with the same properties shown above in the createMicroservice() method, as well as a name property to be used as the injection token. Read more about ClientsModulehere.

  1. @Module({
  2. imports: [
  3. ClientsModule.register([
  4. {
  5. name: 'MATH_SERVICE',
  6. transport: Transport.MQTT,
  7. options: {
  8. url: 'mqtt://localhost:1883',
  9. }
  10. },
  11. ]),
  12. ]
  13. ...
  14. })

Other options to create a client (either ClientProxyFactory or @Client()) can be used as well. You can read about them here.

Context

In more sophisticated scenarios, you may want to access more information about the incoming request. When using the MQTT transporter, you can access the MqttContext object.

  1. @MessagePattern('notifications')
  2. getNotifications(@Payload() data: number[], @Ctx() context: MqttContext) {
  3. console.log(`Topic: ${context.getTopic()}`);
  4. }
  1. @Bind(Payload(), Ctx())
  2. @MessagePattern('notifications')
  3. getNotifications(data, context) {
  4. console.log(`Topic: ${context.getTopic()}`);
  5. }

Hint@Payload(), @Ctx() and MqttContext are imported from the @nestjs/microservices package.

To access the original mqtt packet, use the getPacket() method of the MqttContext object, as follows:

  1. @MessagePattern('notifications')
  2. getNotifications(@Payload() data: number[], @Ctx() context: MqttContext) {
  3. console.log(context.getPacket());
  4. }
  1. @Bind(Payload(), Ctx())
  2. @MessagePattern('notifications')
  3. getNotifications(data, context) {
  4. console.log(context.getPacket());
  5. }

Wildcards

A subscription may be to an explicit topic, or it may include wildcards. Two wildcards are available, + and #. + is a single-level wildcard, while # is a multi-level wildcard which covers many topic levels.

  1. @MessagePattern('sensors/+/temperature/+')
  2. getTemperature(@Ctx() context: MqttContext) {
  3. console.log(`Topic: ${context.getTopic()}`);
  4. }
  1. @Bind(Ctx())
  2. @MessagePattern('sensors/+/temperature/+')
  3. getTemperature(context) {
  4. console.log(`Topic: ${context.getTopic()}`);
  5. }