Subscribing to messages
Warning
Yandex IoT Core is no longer available to new users.
Current users can create resources until November 1, 2026. Afterwards, the service will go read-only and cease to operate on December 1, 2026. For more information on the timing and procedure, see Service shutdown.
With a broker, you can subscribe to any topic that does not start with $ using either a certificate or username and password.
For registries and devices, the available topics are limited. You can subscribe:
- Registry to device events using the
$devices/<device_ID>/eventsor$registries/<registry_ID>/eventstopics. - Registry to device events using the
$devices/<device_ID>/stateor$registries/<registry_ID>/stateretained topics. - Device to registry commands using the
$devices/<device_ID>/commandsor$registries/<registry_ID>/commandstopics. - Device to registry commands using the
$devices/<device_ID>/configor$registries/<registry_ID>/configretained topics. - Registry or device to device monitoring data using the
$monitoring/<device_ID>/jsontopic.
To learn more about messaging, see Sending messages.
Warning
Registry topics and device topics are independent of each other. If a device sends data to a device telemetry topic, you can only receive this data by subscribing to that topic. The same applies to registry topics.
Getting started
Set up resources:
- Optionally, create registry and device certificates.
- Create a registry.
- Add a certificate to the registry.
- Create a device.
- Add a certificate to the device.
Authentication
To send messages, you need to get authenticated. You can use an X.509 certificate or a username and password for authentication. This section provides examples for both authentication methods.
Subscribing a registry to device topics
You can subscribe a registry to topics of one, multiple, or all devices added to it.
Note
If you encounter an error while running a command, add the --debug flag and retry the command. This flag outputs a debug log when running the command, which helps troubleshoot the issue.
Subscribe a registry to a single device's topic
If you do not have the Yandex Cloud CLI yet, install and initialize it.
The folder used by default is the one specified when creating the CLI profile. To change the default folder, use the yc config set folder-id <folder_ID> command. You can also specify a different folder for any command using --folder-name or --folder-id. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.
-
Subscribe a registry to a device topic using certificate-based authentication:
yc iot mqtt subscribe \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$devices/<device_ID>/events' \ --qos 1 -
Subscribe a registry to a device’s retained topic using certificate-based authentication:
yc iot mqtt subscribe \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$devices/<device_ID>/state' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Device topic for sending data.--message: Message text.--qos: Quality of service (QoS) level.
-
Subscribe a registry to a device topic using username and password authentication:
yc iot mqtt subscribe \ --username <registry_ID> \ --password <registry_password> \ --topic '$devices/<device_ID>/events' \ --qos 1 -
Subscribe a registry to a device's retained topic using username and password authentication:
yc iot mqtt subscribe \ --username <registry_ID> \ --password <registry_password> \ --topic '$devices/<device_ID>/state' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Device topic for sending data.--message: Message text.--qos: Quality of service (QoS) level.
Subscribe a registry to the topics of all devices added to it
The registry will only receive data from devices that send messages to the $registries/<registry_ID>/events or $registries/<registry_ID>/state topic.
-
Subscribe a registry to topics of all devices using certificate-based authentication:
yc iot mqtt subscribe \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$registries/<registry_ID>/events' \ --qos 1 -
Subscribe a registry to retained topics of all devices using certificate-based authentication:
yc iot mqtt subscribe \ --cert registry-cert.pem \ --key registry-key.pem \ --topic '$registries/<registry_ID>/state' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Registry topic for receiving data.--message: Message text.--qos: Quality of service (QoS) level.
-
Subscribe a registry to topics of all devices using username and password authentication:
yc iot mqtt subscribe \ --username <registry_ID> \ --password <registry_password> \ --topic '$registries/<registry_ID>/events' \ --qos 1 -
Subscribe a registry to retained topics of all devices using username and password authentication:
yc iot mqtt subscribe \ --username <registry_ID> \ --password <registry_password> \ --topic '$registries/<registry_ID>/state' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Registry topic for receiving data.--message: Message text.--qos: Quality of service (QoS) level.
Subscribing a device to registry topics
Commands from a registry can be given to a specific device or all devices in the registry. This involves using different topics.
Note
If you encounter an error while running a command, add the --debug flag and retry the command. This flag outputs a debug log when running the command, which helps troubleshoot the issue.
Subscribing a device to topics for device-specific commands
-
Subscribe your device to topics for device-specific commands using certificate-based authentication:
yc iot mqtt subscribe \ --cert device-cert.pem \ --key device-key.pem \ --topic '$devices/<device_ID>/commands' \ --qos 1 -
Subscribe your device to retained topics for device-specific commands using certificate-based authentication:
yc iot mqtt subscribe \ --cert device-cert.pem \ --key device-key.pem \ --topic '$devices/<device_ID>/config' \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Device topic for receiving commands.--message: Message text.--qos: Quality of service (QoS) level.
-
Subscribe your device to topics for device-specific commands using username and password authentication:
yc iot mqtt subscribe \ --username <device_ID> \ --password <device_password> \ --topic '$devices/<device_ID>/commands' \ --qos 1 -
Subscribe a device to permanent topics that are commands for a specific device using username and password authorization:
yc iot mqtt subscribe \ --username <device_ID> \ --password <device_password> \ --topic '$devices/<device_ID>/config' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Device topic for receiving commands.--message: Message text.--qos: Quality of service (QoS) level.
Subscribe a device to topics that are commands for all devices
Only devices subscribed to the $registries/<registry_ID>/commands or $registries/<registry_ID>/config topic will receive commands.
-
Subscribe a device to command topics using certificate-based authentication:
yc iot mqtt subscribe \ --cert device-cert.pem \ --key device-key.pem \ --topic '$registries/<registry_ID>/commands' \ --qos 1 -
Subscribe a device to retained command topics using certificate-based authentication:
yc iot mqtt subscribe \ --cert device-cert.pem \ --key device-key.pem \ --topic '$registries/<registry_ID>/config' \ --qos 2Where:
--certand--key: Certificate-based authentication parameters.--topic: Registry topic for sending commands.--message: Message text.--qos: Quality of service (QoS) level.
-
Subscribe a device to command topics using username and password authentication:
yc iot mqtt subscribe \ --username <device_ID> \ --password <device_password> \ --topic '$registries/<registry_ID>/commands' \ --qos 1 -
Subscribe a device to retained command topics using username and password authentication:
yc iot mqtt subscribe \ --username <device_ID> \ --password <device_password> \ --topic '$registries/<registry_ID>/config' \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Registry topic for sending commands.--message: Message text.--qos: Quality of service (QoS) level.
Subscribe to broker topics
When using a broker, you can subscribe to a random topic using your username and password or any certificate assigned to the broker for authentication.
-
Subscribe to a topic using username and password authentication:
yc iot mqtt subscribe \ --username <broker_ID> \ --password <broker_password> \ --topic /my/custom/topic \ --qos 1Where:
--usernameand--password: Username and password authentication parameters.--topic: Topic name that matches the MQTT specification and does not start with$.--qos: Quality of service (QoS) level.
-
Subscribe to a topic using certificate-based authentication:
yc iot mqtt subscribe \ --cert broker-cert.pem \ --key broker-key.pem \ --topic /my/custom/topic \ --qos 1Where:
--certand--key: Certificate-based authentication parameters.--topic: Topic name that matches the MQTT specification and does not start with$.--qos: Quality of service (QoS) level.