Publishes a message to a specified device by using a custom topic.

Limits

  • The Pub operation cannot be used to send commands of setting properties or calling services. If you need to set properties, use the SetDeviceProperty or SetDevicesProperty operation. If you need to call services, use the InvokeThingService or InvokeThingsService operation.
  • Each Alibaba Cloud account can run a maximum of 1600 queries per second (QPS).
    Note RAM users of an Alibaba Cloud account share the quota of the account.

Debugging

OpenAPI Explorer automatically calculates the signature value. For your convenience, we recommend that you call this operation in OpenAPI Explorer. OpenAPI Explorer dynamically generates the sample code of the operation for different SDKs.

Request parameters

Parameter Type Required Example Description
Action String Yes Pub

The operation that you want to perform. Set the value to Pub.

MessageContent String Yes aGVsbG8gd29ybGQ%3D

The body of the message to be published.

To generate a message body, you must convert the raw message into binary data and perform Base64 encoding.

ProductKey String Yes a1Q5XoY****

The ProductKey of the device that receives the message.

TopicFullName String Yes /a1Q5XoY****/device1/user/get

The custom topic that is used to publish the message. Topic format: /${productKey}/${deviceName}/user/${TopicShortName}.

Note You must specify the Subscribe permission, or Publish and Subscribe permissions for the topic. Make sure that the device subscribes to the topic.

You can view the custom topics of a product on the Topic Categories tab of the Product Details page, or by calling the QueryProductTopic operation. You can also view the topics to which the device subscribes on the Topic list tab of the Device Details page.

IotInstanceId String No iot-cn-0pp1n8t****

The ID of the instance. This parameter is not required for the public instance but required for Enterprise Edition instances.

Qos Integer No 0

The quality of service (QoS) level of the message. Valid values:

  • 0: The message is published at most once.
  • 1: The message is published at least once.

Default value: 0

Note IoT Platform stores QoS 1 messages for up to 7 days. IoT Platform does not store QoS 0 messages.
ResponseTopic String No /a1Q5XoY****/device1/user/update

The response topic in the request/response communication mode when you use MQTT 5.0.

CorrelationData String No aGVsbG8****

The related data in the request/response communication mode when you use MQTT 5.0. You can specify this parameter as needed.

A message recipient can process the request based on the data.

Note You must convert the related data into binary data and perform Base64 encoding to generate a value of the string type.
UserProp.N.Key String No key1

The custom property key that is specified when you use MQTT 5.0 for communication.

This parameter must be used in pair with the UserProp.N.Value parameter.

UserProp.N.Value String No value1

The custom property value that is specified when you use MQTT 5.0 for communication.

This parameter must be used in pair with the UserProp.N.Key parameter.

In addition to the preceding operation-specific request parameters, you must specify common request parameters when you call this operation. For more information about common request parameters, see Common parameters.

Response parameters

Parameter Type Example Description
Code String iot.system.SystemException

The error code returned if the call fails. For more information, see Error codes.

ErrorMessage String A system exception occurred.

The error message returned if the call fails.

MessageId String 889455942124347329

The ID of the message. The message ID is generated by IoT Platform when the message is sent.

RequestId String BB71E443-4447-4024-A000-EDE09922891E

The ID of the request.

Success Boolean true

Indicates whether the call was successful.

  • true: The call was successful.
  • false: The call failed.

Examples

Sample requests

https://iot.cn-shanghai.aliyuncs.com/?Action=Pub
&ProductKey=a1Q5XoY****
&TopicFullName=/a1Q5XoY****/device1/user/get
&MessageContent=aGVsbG8gd29ybGQ%3D
&Qos=0
&ResponseTopic=/a1Q5XoY****/device1/user/update
&CorrelationData=aGVsbG8%3D****
&UserProp.1.Key=k1&UserProp.1.Value=v1
&<Common request parameters>

Sample success responses

XML format

<PubResponse>
    <RequestId>BB71E443-4447-4024-A000-EDE09922891E</RequestId>
    <Success>true</Success>
    <MessageId>889455942124347329</MessageId>
</PubResponse>

JSON format

{
      "RequestId":"BB71E443-4447-4024-A000-EDE09922891E",
      "Success":true,
      "MessageId":889455942124347329
  }

Error codes

For a list of error codes, visit the API Error Center.