Skip to content

About

A library which queries and processes bootstrap data from a bootstrap server.

Resources

Stars

0 stars

Watchers

11 watching

Forks

Repository files navigation

Bootstrap Client

Queries bootstrap data from the bootstrap server and initializes an MQTT client and a logger for services.

Usage

Install this library via npm:

npm install @artcom/bootstrap-client

This package now targets Node.js 26+ and is published as ESM.

Bootstrap as follows:

import { init, subscribeToConfigChange } from "@artcom/bootstrap-client"

const { logger, mqttClient, queryConfig, data } = await init(bootstrapUrl, serviceId)

// log something
logger.info("Hello world!")

// publish "bar" to topic "foo"
mqttClient.publish("foo", "bar")

// query some configurations with (optional) options
const myConfig = await queryConfig("config/path", {
  version: "master",
  listFiles: false,
  includeCommitHash: false,
  parseJSON: true,
})

// use raw bootstrap data
logger.info(`I am running on device: ${data.device}`)

// subscribe to configuration changes
subscribeToConfigChange(
  mqttClient,
  "device/config/changes", // The topic to listen on
  ["config.json", "assets/"], // Watch specific files or directories
  "master", // The config version to match
  async () => {
    logger.info("Configuration changed, reloading...")
  }
)

The following additional options are supported:

  • timeout: is the time in milliseconds until the query times out, default: 2000
  • retryDelay: is the time in milliseconds until the query is sent again, default: 10000
  • debugBootstrapData: can be set to skip querying data from the bootstrap server for debugging, default: null
  • mqttOptions: options passed to the MQTT client, merged after the generated clientId. Either an object or a function (sync or async) that is called once after the bootstrap data was retrieved and before connecting. It receives { logger, data, httpClient, queryConfig }, so options can depend on configuration, e.g. a last will:
const { mqttClient } = await init<{ configServerUri: string }>(bootstrapUrl, serviceId, {
  mqttOptions: async ({ queryConfig }) => {
    const { willTopic } = await queryConfig<{ willTopic: string }>("device/config.json")

    return {
      will: { topic: willTopic, payload: "offline", qos: 1, retain: true, stringifyJson: false },
    }
  },
})

queryConfig is undefined if the bootstrap data has no configServerUri; pass a type with configServerUri: string to init (as above) to make it non-optional.

Consumers must run on Node.js 26 or newer and import the package from an ESM environment.

Bootstrap Data

The client expects the following properties in the bootstrap server response or debugBootstrapData:

{
  tcpBrokerUri: string,
  httpBrokerUri: string,
  configServerUri: string,
  device: string
}

Checkout the bootstrap server documentation for details.

About

A library which queries and processes bootstrap data from a bootstrap server.

Resources

Stars

0 stars

Watchers

11 watching

Forks

Releases

Packages

Used by

Contributors

Languages