npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lb4-extension-mqtt

v0.2.1

Published

LoopBack 4 MQTT Extension

Downloads

10

Readme

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

| MqttBinding | Binding Key | What we need | | ------------- | :---------------------------------------: | ---------------------------------------------------------: | | MQTT_CONFIG | ${CoreBindings.APPLICATION_CONFIG}.mqtt | Configuration, see MqttServerConfig | | MQTT_EXCHANGE | mqtt.exchange | MQTT Exchange | | MQTT_QUEUE | mqtt.queue | MQTT 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:

| MqttBinding | Binding Key | | ------------- | :-----------: | | MQTT_PROVIDER | mqtt.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