nestjs-mqtt
v1.5.0
Published
A MQTT module for Nest.js
Downloads
7
Maintainers
Readme
NestJS-MQTT
支持nestjs 11 版本
Description
A MQTT module for Nest.js. Compatible with emqtt.
Installation
⚠️ After version 0.2.0,
nestjs-mqttmake a breaking change. User should add additionalmqttpackage manual. @nestjs/core and @nestjs/common version >= 11.0.1 is required.
⚠️ 支持 @nestjs/core @nestjs/common 11.0.1
$ npm install nestjs-mqtt mqtt --saveUsage
Import
nestjs-mqtt will register as a global module.
You can import with configuration
version 1.3.0 新增 topic 动态传参
// app.module.ts
import { Module } from '@nestjs/common';
import { MqttModule } from 'nestjs-mqtt';
@Module({
imports: [MqttModule.forRoot({
...,
variables: {
hello: 'test'
}
})]
})
export class AppModule {}
import { Injectable } from '@nestjs/common';
import { Subscribe, Payload, Topic } from 'nestjs-mqtt';
@Injectable()
export class TestService {
@Subscribe('{{hello}}/test')
test() {
}
@Subscribe({
topic: '{{hello}}/test2',
transform: payload => payload.toString(),
})
test2() {
}
}version 1.2.0 新增 动态加载配置文件建立连接,配置文件为json,文件内容为mqtt连接配置
// app.module.ts
import { Module } from '@nestjs/common';
import { MqttModule } from 'nestjs-mqtt';
@Module({
imports: [
MqttModule.forRoot({
load: 'filename',
}),
],
})
export class AppModule {}// app.module.ts
import { Module } from '@nestjs/common';
import { MqttModule } from 'nestjs-mqtt';
@Module({
imports: [MqttModule.forRoot(options)],
})
export class AppModule {}or use async import method
// app.module.ts
import { Module } from '@nestjs/common';
import { MqttModule } from 'nestjs-mqtt';
@Module({
imports: [
MqttModule.forRootAsync({
useFactory: () => options,
}),
],
})
export class AppModule {}Subscribe
You can define any subscriber or consumer in any provider. For example,
import { Injectable } from '@nestjs/common';
import { Subscribe, Payload, Topic } from 'nestjs-mqtt';
@Injectable()
export class TestService {
@Subscribe('test')
test() {}
@Subscribe({
topic: 'test2',
transform: (payload) => payload.toString(),
})
test2() {}
}Also, you can inject parameter with decorator:
import { Injectable } from '@nestjs/common';
import { Subscribe, Payload } from 'nestjs-mqtt';
@Injectable()
export class TestService {
@Subscribe('test')
test(@Payload() payload) {
console.log(payload);
}
}Here are all supported parameter decorators:
Payload(transform?: (payload) => any)
Get the payload data of incoming message. You can pass in a transform function for converting.
Topic()
Get the topic of incoming message.
Packet()
Get the raw packet of incoming message.
Params()
Get the wildcard part of topic. It will return an array of string which extract from topic. For example:
When subscribe the topic "test/+/test/+" and incoming topic is "test/1/test/2", you will get the array ["1", "2"].
Publish
nestjs-mqtt wrap some functions with Promise and provide a provider.
import { Inject, Injectable } from '@nestjs/common';
import { MqttService } from 'nestjs-mqtt';
@Injectable()
export class TestService {
constructor(@Inject(MqttService) private readonly mqttService: MqttService) {}
async testPublish() {
this.mqttService.publish('topic', {
foo: 'bar',
});
}
}Emqtt Compatible
nestjs-mqtt support emq shared subscription
- Global mode
Module options support queue and share property for globally converting all topic to shared topic except configured in subscription options.
// app.module.ts
import { Module } from '@nestjs/common';
import { MqttModule } from 'nestjs-mqtt';
@Module({
imports: [
MqttModule.forRoot({
host: '127.0.0.1',
queue: true,
share: 'group1',
}),
],
})
export class AppModule {}- Configure in Subscribe
import { Injectable } from '@nestjs/common';
import { Subscribe, Payload, Topic } from 'nestjs-mqtt';
@Injectable()
export class TestService {
@Subscribe('test')
test() {}
@Subscribe({
topic: 'test2',
queue: true,
})
test2() {}
}The priority of subscribe is higher than the global mode. If you want to specify a topic do not use the shared mode, set it as false in subscribe decorator.
Support
nestjs-mqtt is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
License
nestjs-mqtt is MIT licensed.
