Updated on 2024-11-06 GMT+08:00

Broadcasting a Message

Function

This API is used by an application to broadcast a message to all online devices that subscribe to a specified topic. After an application delivers a broadcast message to the platform, the platform returns a response to the application and then broadcasts the message to devices. Note:

  • This API applies only to devices that use the MQTT protocol for access.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

POST /v5/iot/{project_id}/broadcast-messages

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Parameter description: project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

Parameter description: user token. You can obtain the token by calling the IAM API Obtaining a User Token Through Password Authentication. In the returned response header, X-Subject-Token is the desired user token. For details about how to obtain the token, see Token Authentication.

Instance-Id

No

String

Parameter description: instance ID. Unique identifier of each instance in the physical multi-tenant scenario. Mandatory for professional editions and recommended in other cases. Log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID. For details, see Viewing Instance Details.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

app_id

No

String

Parameter description: resource space ID. This parameter is optional. If you have multiple resource spaces, you can use this parameter to specify the resource space to which the broadcast message belongs. If this parameter is not specified, the broadcast message will be sent to devices that subscribe the specified topic in the default resource space. Value: The value can contain a maximum of 36 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

topic_full_name

Yes

String

Parameter description: (mandatory) complete name of the topic for receiving broadcast messages. To broadcast messages to devices, you can use this parameter to specify a complete topic name. Then, the IoT platform sends messages to all online devices that subscribe to the topic in the specified resource space. Broadcast topics do not need to be created on the console, but the topic prefix must be $oc/broadcast/.

Maximum: 128

message

Yes

String

Parameter description: content of the broadcast message. You need to encode the original message using Base64. The maximum length of the request body is 256 KB.

Maximum: 261848

ttl

No

Integer

Parameter description: aging time of the caches of broadcasted messages on the platform, in minutes. The default value is 0, indicating that messages are not cached. The value of ttl must be a multiple of 5, that is, the granularity is 5 minutes. The maximum cache duration is 1440 minutes. For longer cache duration, you need to submit a request in advance, or the delivery will fail. If this parameter is set to a value greater than 0, no more than 10 devices are allowed to subscribe to the same topic at the same time, or the API returns an error.

Minimum: 0

Default: 0

message_id

No

String

Parameter description: message ID, which is user-defined (UUID is recommended). If the value of ttl is greater than 0, the platform caches messages. You need to specify a unique value for message_id, or the API returns an error. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (_), and hyphens (-) are allowed.

Maximum: 128

Response Parameters

Status code: 201

Table 4 Response body parameters

Parameter

Type

Description

app_id

String

Parameter description: resource space ID.

topic_full_name

String

Parameter description: complete name of the topic for receiving broadcast messages.

Maximum: 128

message_id

String

Message ID, which is generated by the IoT platform to identify the message.

created_time

String

UTC time when the message was created. The value is in the format of yyyyMMdd'T'HHmmss'Z'.

Example Requests

Broadcasts a message.

POST https://{endpoint}/v5/iot/{project_id}/broadcast-messages

{
  "topic_full_name" : "$oc/broadcast/test",
  "message" : "SGVsbG9Xb3JsZA=="
}

Example Responses

Status code: 201

Created

{
  "app_id" : "jeQDJQZltU8iKgFFoW060F5SGZka",
  "topic_full_name" : "$oc/broadcast/test",
  "message_id" : "b1224afb-e9f0-4916-8220-b6bab568e888",
  "created_time" : "20151212T121212Z"
}

Status Codes

Status Code

Description

201

Created

400

Bad Request

401

Unauthorized

403

Forbidden

404

Not Found

500

Internal Server Error

Error Codes

See Error Codes.