Subscribing a device or registry to receive messages

You can subscribe:

  • A registry to device events using the $devices/<device ID>/events or $registries/<registry ID>/events topics.
  • A device to registry commands using the $devices/<device ID>/commands or $registries/<registry ID>/commands topics.

To learn about messaging, see Sending messages.

Important

Registry and device topics are not interconnected. If a device sends data to the device topic for telemetry data, you can only receive it by subscribing to this topic. The same is true for registry topics.

Before you start

To get started, you will need:

  1. A registry.
  2. A registry certificate.
  3. A device.
  4. A device certificate.

Subscribing a registry to a device

You can subscribe a registry to one, multiple, or all devices added to it. Let's look at all the options.

Subscribe a registry to a device or devices using the following parameters:

  • --cert: Path to the public part of the registry certificate.
  • --key: Path to the public part of the registry certificate.
  • --topic: Device topic.
  • --qos: Quality of service (QoS).

Note

If you encounter an error while running the command, add the --debug flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Subscribe a registry to a single device:

    $ yc iot mqtt subscribe \
    --cert registry-cert.pem \
    --key registry-key.pem \
    --topic '$devices/<device ID>/events' \
    --qos 1
    
  • Subscribe a registry to all devices added to it:

    $ yc iot mqtt subscribe \
    --cert registry-cert.pem \
    --key registry-key.pem \
    --topic '$registries/<registry ID>/events' \
    --qos 1
    

    The registry will only receive data from devices that send messages to the $registries/<registry ID>/events topic.

Subscribing a device to a registry

Commands from a registry can be given to a specific device or all devices in the registry. This involves using different topics.

Subscribe a device to a registry using the following parameters:

  • --cert: Path to the public part of the device certificate.
  • --key: Path to the public part of the device certificate.
  • --topic: Device topic.
  • --qos: Quality of service (QoS).

Note

If you encounter an error while running the command, add the --debug flag to the command and try again. This flag outputs the debug log when running the command, which helps you diagnose the problem.

If you don't have the Yandex.Cloud command line interface yet, install it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

  • Subscribe a device to commands for a specific device:

    yc iot mqtt subscribe \
    --cert device-cert.pem \
    --key device-key.pem \
    --topic '$devices/<device ID>/commands' \
    --qos 1
    
  • Subscribe a device to commands for all devices:

    $ yc iot mqtt subscribe \
    --cert device-cert.pem \
    --key device-key.pem \
    --topic '$registries/<registry ID>/commands' \
    --qos 1
    

    Only devices subscribed to the $registries/<registry ID>/commands topic will receive commands.