Relay MQTT messages between client devices and Amazon IoT Core - Amazon IoT Greengrass
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Relay MQTT messages between client devices and Amazon IoT Core

You can relay MQTT messages and other data between client devices and Amazon IoT Core. Client devices connect to the MQTT broker component that runs on the core device. By default, core devices don't relay MQTT messages or data between client devices and Amazon IoT Core. Client devices can communicate only with each other over MQTT by default.

To relay MQTT messages between client devices and Amazon IoT Core, configure the MQTT bridge component to do the following:

  • Relay messages from client devices to Amazon IoT Core.

  • Relay messages from Amazon IoT Core to client devices.

Note

The MQTT bridge uses QoS 1 to publish and subscribe to Amazon IoT Core, even when a client device uses QoS 0 to publish and subscribe to the local MQTT broker. As a result, you might observe additional latency when you relay MQTT messages from client devices on the local MQTT broker to Amazon IoT Core. For more information about MQTT configuration on core devices, see Configure MQTT timeouts and cache settings.

Configure and deploy the MQTT bridge component

The MQTT bridge component consumes a list of topic mappings that each specify a message source and a message destination. To relay messages between client devices and Amazon IoT Core, deploy the MQTT bridge component, and specify each source and destination topic in the component configuration.

To deploy the MQTT bridge component to a core device or group of core devices, create a deployment that includes the aws.greengrass.clientdevices.mqtt.Bridge component. Specify the topic mappings, mqttTopicMapping, in the MQTT bridge component configuration in the deployment.

The following example defines a deployment that configures the MQTT bridge component to relay messages on topics that match the clients/+/hello/world topic filter from client devices to Amazon IoT Core. The merge configuration update requires a serialized JSON object. For more information, see Update component configurations.

Console
{ "mqttTopicMapping": { "HelloWorldIotCore": { "topic": "clients/+/hello/world", "source": "LocalMqtt", "target": "IotCore" } } }
Amazon CLI
{ "components": { "aws.greengrass.clientdevices.mqtt.Bridge": { "version": "2.0.0", "configurationUpdate": { "merge": "{\"mqttTopicMapping\":{\"HelloWorldIotCore\":{\"topic"\:\"clients/+/hello/world\",\"source\":\"LocalMqtt\",\"target\":\"IotCore\"}}}" } } ... } }

Relay MQTT messages

To relay MQTT messages between client devices and Amazon IoT Core, configure and deploy the MQTT Bridge component and specify the topics to relay.

Example: Relay messages on a topic from client devices to Amazon IoT Core

The following MQTT bridge component configuration specifies relaying messages on topics that match the clients/+/hello/world/event topic filter from client devices to Amazon IoT Core.

{ "mqttTopicMapping": { "HelloWorldEvent": { "topic": "clients/+/hello/world/event", "source": "LocalMqtt", "target": "IotCore" } } }
Example: Relay messages on a topic from Amazon IoT Core to client devices

The following MQTT bridge component configuration specifies relaying messages on topics that match the clients/+/hello/world/event/response topic filter from Amazon IoT Core to client devices.

{ "mqttTopicMapping": { "HelloWorldEventConfirmation": { "topic": "clients/+/hello/world/event/response", "source": "IotCore", "target": "LocalMqtt" } } }