Git Product home page Git Product logo

nest-mqtt's Introduction

Nest-MQTT

Description

A MQTT module for Nest.js. Compatible with emqtt.

Installation

$ npm install @fetaoily/nest-mqtt mqtt --save

or

$ yarn add @fetaoily/nest-mqtt mqtt

Usage

Import

Nest-mqtt will register as a global module.

You can import with configuration

// app.module.ts
import {Module} from '@nestjs/common';
import {MqttModule} from '@fetaoily/nest-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 '@fetaoily/nest-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 '@fetaoily/nest-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 '@fetaoily/nest-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

Nest-mqtt wrap some functions with Promise and provide a provider.

import {Inject, Injectable} from '@nestjs/common';
import {MqttService} from '@fetaoily/nest-mqtt';

@Injectable()
export class TestService {
  constructor(
    @Inject(MqttService) private readonly mqttService: MqttService,
  ) {
  }

  async testPublish() {
    this.mqttService.publish('topic', {
      foo: 'bar'
    });
  }

}

Emqtt Compatible

nest-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 '@fetaoily/nest-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 '@fetaoily/nest-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

Nest-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.

Stay in touch

License

nest-mqtt is MIT licensed.

nest-mqtt's People

Contributors

fetaoily avatar jeniturtle avatar microud avatar shenqidebaozi avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.