0.2.1 • Published 5 years ago

lb4-extension-mqtt v0.2.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

lb4-extension-mqtt

An MQTT extension for LoopBack 4.

Installation

Run the following command to install lb4-extension-mqtt:

npm i -s lb4-extension-mqtt

Usage

When the lb4-extension-mqtt package is installed, bind it to your application with app.component()

import {RestApplication} from '@loopback/rest';
import {MqttComponent} from 'lb4-extension-mqtt';

const app = new RestApplication();
// Keep in mind that some extra configuration is required
// as shown in the following steps
app.component(MqttComponent);

Configuration

To make a connection, you'll have to bind some values that will be provided to the extension.

Tip: Use MqttBinding from lb4-extension-mqtt/dist10

MqttBindingBinding KeyWhat we need
MQTT_CONFIG${CoreBindings.APPLICATION_CONFIG}.mqttConfiguration, see MqttServerConfig
MQTT_EXCHANGEmqtt.exchangeMQTT Exchange
MQTT_QUEUEmqtt.queueMQTT Queue

Mqtt Server Config

The MQTT Server config holds the configuration for the server connection.

Tip: Use MqttServerConfig type from lb4-extension-mqtt/dist10/types

const config: MqttServerConfig = {
  protocol: process.env.MQTT_PROTOCOL || 'amqp://',
  vhost: process.env.MQTT_VHOST || 'someVhost',
  host: process.env.MQTT_HOST || 'localhost',
  port: process.env.MQTT_PORT === undefined ? 8883 : +process.env.MQTT_PORT,
  user: process.env.MQTT_USER || 'user',
  pass: process.env.MQTT_PASS || 'pass',
};

this.bind(MqttBinding.CONFIG).to(config);

Retrieve messages

When a message is received from the message queue, it is stored in an array with type Message[] where Message is imported from amqplib.

The extension exports the messages using a Provider. You can use the inject the value of this provider using the following key:

MqttBindingBinding Key
MQTT_PROVIDERmqtt.provider

When injecting in your class, you'll only receive the value once since you can't inject the value multiple times in the same instance.

An example to retrieve the value with an interval:

import {MqttBinding} from 'lb4-extension-mqtt/dist10';

class CheckMqttComponent {
  // This will inject the MqttComponent's provider
  constructor(@inject(MqttBinding.MQTT_PROVIDER) public provider: Message[]) {}

  getMessages(): Message[] {
    // This will return a list of all received messages
    // since the last time you called this function
    return this.provider;
  }
}

class SomeOtherClass {
  constructor(
    @inject(CoreBindings.APPLICATION_INSTANCE) private app: Application,
  ) {
    this.checkForNewMqttMessages();
  }

  private checkForNewMqttMessages() {
    setInterval(async () => {
      const checkMqttComponent: CheckMqttComponent = new CheckMqttComponent(
        await this.app.get(MqttBinding.MQTT_PROVIDER),
      );

      const messages: Message[] = checkMqttComponent.getMessages();
      for (let message of messages) {
        // Do something amazing with it!
      }
    }, 15000);
  }
}

Development & issues

If you need help with using this extension, feel free to open an issue.

If you think that this extension could use some improvements, feel free to open a PR.

License

MIT

LoopBack

0.2.1

5 years ago

0.2.0-2

5 years ago

0.2.0-1

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago