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()
;; ;// Keep in mind that some extra configuration is required// as shown in the following stepsapp.componentMqttComponent;
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
; this.bindMqttBinding.CONFIG.toconfig;
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:
;
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.