Docs
MQTT
MQTT is a reliable, lightweight protocol with a small code and network bandwidth footprint. The platform provides a native integration to publish measurements and subscribe to property updates (e.g. for updating device settings) over MQTT.
In order to connect an MQTT client the following configuration can be used to connect to the Blockbax MQTT server:
| Property | Value |
|---|---|
| Broker host | mqtt.blockbax.com |
| Port number | 8883 (default secure MQTT port) |
| Protocols | MQTT 3.1, 3.1.1 or 5 over TLS version 1.2 or 1.3. Currently MQTT 5 features such as topic aliases, subscription identifiers, and shared subscriptions are not supported. If your use case requires these features, please contact us. |
| Username | The public ID of your access key |
| Password | The secret of your access key |
| Client ID | Only one connection is allowed per client ID and access key combination, if a connection already exists for this combination when a new connection is attempted, the existing client is disconnected (see 3.1.4 of MQTT standard). Due to the distributed nature of the Blockbax Platform there could be lost / concurrent sessions in exceptional cases. |
| Keep alive interval | 60 seconds (default for most clients) or less |
| Quality of Service (QoS) | 1 (at least once) or 0 (at most once) |
Inbound connectors
Inbound connectors are integrations to ingest measurements into the Blockbax Platform. In the case of MQTT, there is a default connector that can be reached by connecting to Blockbax and publishing on the following topic:
v1/projects/<YOUR_PROJECT_ID>/measurements
This topic expects measurements in the format described in our API reference documentation.
If the payload format is different from the default format, you can use a custom inbound connector. A custom inbound connector has a payload conversion script that is defined as a plain JavaScript function. A dedicated page about payload conversion describes how to develop such a function. This also allows you to log certain things about your payload. A custom inbound connector can be reached on the following topic:
v1/projects/<YOUR_PROJECT_ID>/inboundConnectors/<INBOUND_CONNECTOR_ID>/measurements
In addition to a push-based connector where you publish measurements to Blockbax on a certain topic, it is also possible to define a pull-based MQTT connector. This will stream measurements from an external MQTT broker into Blockbax
The following configuration options are available:
| Field | Description |
|---|---|
| State | Active or Inactive |
| Name | A descriptive name to recognize and find your inbound connector |
| Auto-create subjects | Automatically creates a subjects based on the incoming payload (see below) |
| Broker host | The broker host to connect to, e.g. mqtt.example.com |
| Broker port | The broker port to connect to |
| Topic | The topic to subscribe to, e.g. room/+/temperature/+ |
| Client ID | The client ID the client will connect with |
| Trust store certificate chain | The server certificate that will have to be trusted by the client (has to be in the pem format). When set, CA certificates won’t be respected anymore. |
| Username | The username that the client will connect with |
| Password | The password that the client will connect with |
Auto-create subjects
Measurements are mapped to subjects and metrics by so-called ingestion IDs. By default, an ingestion ID is derived from the subjects’ external ID and the metrics’ external ID (e.g. MyCar$Location). You can also override this default with a custom ingestion ID. If a measurement with an unknown ingestion ID is encountered, Blockbax tries to find a single metric with an external ID equal to the part after the dollar sign. If this is successful, a subject will be created with an external ID equal to the part before the dollar sign. For example, for the ingestion ID MyCar$Location a subject with external ID MyCar will be created if the metric with external ID Location can be linked to exactly one subject type.
Integration templates
Inbound connectors can also be configured from a integration template. To make integrating with Blockbax as easy as possible we provide integration templates that can integrate any available third party platform or device with a few clicks. We use integration templates in most of our connection guides.
Outbound connectors
The connected client can consume properties for one or multiple subjects, to do this you can either subscribe to a specific subject and property combination or use wildcards, denoted with a + symbol, to subscribe to multiple properties or subjects simultaneously. For subscribing to single properties and/or properties you can use your own configured external IDs. This way clients can use their own naming instead of having to know which IDs are used internally by the Blockbax Platform. The topics that can be used are listed below:
- To subscribe to a specific property and subject combination use:
v1/projects/<YOUR_PROJECT_ID>/subjects/<YOUR_SUBJECT_EXTERNAL_ID>/properties/<YOUR_PROPERTY_TYPE_EXTERNAL_ID> - To subscribe to all properties of a single subject use:
v1/projects/<YOUR_PROJECT_ID>/subjects/<YOUR_SUBJECT_EXTERNAL_ID>/properties/+ - To subscribe to a single property for all subjects use:
v1/projects/<YOUR_PROJECT_ID>/subjects/+/properties/<YOUR_PROPERTY_TYPE_EXTERNAL_ID> - To subscribe to all properties for all subjects use:
v1/projects/<YOUR_PROJECT_ID>/subjects/+/properties/+
Note that your project ID can be found in the URL when you open your project in the web app, e.g. app.blockbax.com/projects/40edc099-7a41-4af3-9fa4-2fa4ac23a87a/
Important note about wildcards and permissions
You can only make use of wildcards if you use an access key with read access to all properties and/or subjects.
Important note about topics
MQTT topics restrict the use of the following symbols: #, /, + and spaces. If your external subject ID or your external property type ID contains one of these symbols the topics can not be created, so you can not receive property updates for these resources.
Messages consumed by the connected client are JSON UTF-8 encoded strings and contain either a text, number or a location value (see examples below). The latest non-empty message per topic is retained by our MQTT broker such that upon connection the client receives the latest property value. Additionally you must design your system in such a way that it automatically reconnects and it can cope with receiving the same update twice (at-least-once delivery semantics). A message can be delivered one or more times due to network problems, rolling broker restarts and the asynchronous nature of the platform.
This is an example of a message containing text:
| |
This is an example of a message containing a number:
| |
This is an example of a message containing a location:
| |
When you remove a property value, either from a subject or entirely, a message containing an empty string will be produced. Take note that in the end this information is cleaned up, so when you resubscribe, it might not be sent as a retained message anymore and the topic might no longer be there.
Connection guide
Here you can find the information to help you get started quickly with this integration. In this tutorial we use the HiveMQ as an example. This guide should be usable for self-hosted or other managed MQTT brokers as well, in case you need help contact us.
Stream measurements from HiveMQ to Blockbax
Several steps in both HiveMQ and Blockbax are needed to get the data from your MQTT broker to Blockbax.
- In HiveMQ’s access management create credentials to subscribe to the (part) the required topic(s) or with a generic ‘Subscribe Only’ permission. Copy the settings, you need it in the following step.
- Create an inbound connector for MQTT in Blockbax and choose ‘MQTT subscribe’ as protocol.
- Validate if an info log appears that it is able to connect successfully. This can take up to 30 seconds.