@vocovo/sparkplug-host-app
v1.7.0
Published
Implementation of Sparkplug host app in JavaScript
Downloads
494
Maintainers
Keywords
Readme
@vocovo/sparkplug-host-app
Provides reusable logic for Sparkplug Host Applications.
Intentionally designed to be:
- Not tied to a particular MQTT Client implementation
- this makes it easier to unit test
- and gives users of this package more flexibility to use other clients
- To not be concerned with how different Sparkplug messages are handled
- so that different hosts can implement only the logic they need
Semantic Release
This repo uses Semantic Release. Please ensure your PRs are Conventional Commits compliant in order for the release / deploy CD workflow to run on merge to main.
Installation
npm install @vocovo/sparkplug-host-app
Usage
import { connectAsync } from 'mqtt'
import { UPayload } from 'sparkplug-payload/lib/sparkplugbpayload'
import { SparkplugHostApplication, EVENTS, ParsedTopic } from '@vocovo/sparkplug-host-app'
const main = async () => {
const host = new SparkplugHostApplication('hostId')
// Instantiate your own MQTT client
const mqttClient = await connectAsync('mqtt://localhost:1883', {
will: sparkplugHostApp.getOrInitializeWill(),
})
// Making your MQTT client compatible with the MQTTClient interface
sparkplugHostApp.setMqttClient({
on: mqttClient.on.bind(mqttClient),
subscribe: mqttClient.subscribeAsync.bind(mqttClient),
publish: mqttClient.publishAsync.bind(mqttClient),
})
await host.start()
host.on(EVENTS.NBIRTH, (parsedTopic: ParsedTopic, decodedPayload: UPayload) => {
// Will be called whenever an NBIRTH message is received
// Handle it however you want
})
host.on(EVENTS.NDEATH, (parsedTopic: ParsedTopic, decodedPayload: UPayload) => {
// Will be called whenever an NDEATH message is received
// Handle it however you want
})
host.on(EVENTS.NDATA, (parsedTopic: ParsedTopic, decodedPayload: UPayload) => {
// Will be called whenever an NDATA message is received
// Handle it however you want
})
// To terminate the session correctly, we should eventually call the 'shutdown' method
await host.shutdown()
}
main()
Using yalc in your local development workflow
You can use yalc to test using this package with other repos.
You will first need to run npm run build
then yalc publish
inside this package.
Then run yalc add @vocovo/sparkplug-host-app
in the relevant repo.
You can then use yalc update @vocovo/sparkplug-host-app
when making changes, but you will need to re run npm run build
every time.
If developing inside a docker container you will also need to copy the .yalc
folder across by running COPY ./.yalc/ ./.yalc/
you may also need to run npm install
in the root dir to update the lock file.