Publishing a Message

Last updated: 2019-07-05 15:11:00

PDF

1. API Description

This AP (PublishMessage) is used to publish a message to a specified topic.

Domain for public network API request: cmq-topic-region.api.qcloud.com

Domain for private network API request: cmq-topic-region.api.tencentyun.com

Whenever (including during alpha test) any public network downstream traffic is generated from the use of a public network domain, a fee will be charged. It is strongly recommended that users whose services are on the Tencent Cloud use private network domains, because no fee will be charged for the traffic consumed in the private network.

  • region should be replaced with a specific region: gz (Guangzhou), sh (Shanghai), or bj (Beijing). The region value in the common parameters should be consistent with the region value of the domain. If there is an inconsistency, the request will be sent to the region specified by the domain.
  • Public network domain requests both support http and https. Private network requests only support http.
  • Some of the input parameters are optional. If not specified, the default value will be taken.
  • All the output parameters will be returned to the user when the request is successful; otherwise, at least code, message, and requestId will be returned.

2. Input Parameters

The following request parameter list only provides API request parameters. For other parameters, refer to Common Request Parameters.

Parameter Name Required Type Description
topicName Yes String Topic name. It is unique under the same account in one region. The topic name is a string of no more than 64 characters, which can contain letters, numbers, and hyphens (-) and must begin with a letter.
msgBody Yes String Message body. The minimum length is 1 byte, and the maximum length depends on the set maximum length attribute of the topic message.
msgTag.n No String Tag for message filtering. Message tag (used to filter messages). The maximum number of tags is 5, with each containing not more than 16 characters. It is used in combination with the filterTag parameter of API Subscribe, and the rules are: 1) if filterTag is not configured, subscribers can receive all the messages published on Topic no matter whether msgTag is configured; 2) if filterTag array is specified, subscribers can receive the messages published on Topic only when at least one value in filterTag array also exists in the msgTag array (that is, there is an intersection between filterTag and msgTag); 3) if filterTag array is specified, but msgTag is not configured, subscribers cannot receive any messages published on Topic. 3) can be considered as a special case of 2), and there is no intersection between filterTag and msgTag in this case. The rules are designed to allow subscribers to choose at their own discretion.
routingKey No String This field indicates the routing path for sending messages. Its length should be not more than 64 bytes and contain up to 15 ".", i.e. 16 phrases at most.
The "routingKey" cannot be specified randomly when messages are sent to the "exchange" of topic type. The above format requirement must be met. A message with routingKey specified by a subscriber will be pushed to all the consumers whose BindingKey matches with routingKey. There are two matching options:
1 * (asterisk) can be a substitute for a word (a sequence of alphabetic string);
2 # (pound sign) can be used to match one or more characters.

3. Output Parameters

Parameter Name Type Description
code Int 0: Succeeded, others: Error. See the table below for specific errors.
message String Error message.
requestId String Request ID generated by server. When there is an internal error on the server, users can submit this ID to the backend to locate the problem.
msgId String Unique ID of the message generated by the server.

4. Error Codes

Error Code Module Error Code Error Message Description
4000 10490 number of filterTag exceed limit The number of filterTag exceeds the limit. Currently, the maximum is 5.
6030 10730 no bindingKey or filterTag matches the routingKey or msgTag No subscription matches the message tag or routingKey for this publication. Therefore, the message published this time will not be delivered to subscribers.
6030 10650 topic has no subscription, please create a subscription before publishing message There is no subscriber under this topic. Please create a subscriber before publishing a message.
4000 10700 parameters lack of routingKey The routingKey parameter is missing.
4000 10720 too many msgTag There are too many msgTag.

Note: The error codes listed in the above table are specific to this API. If the error code you are looking for is not in the table, you may find it in the Common Error Codes.

5. Example

Input:

https://domain/v2/index.php?Action=PublishMessage
&topicName=test-topic-123
&msgBody=helloworld
&<Common request parameters>

Output:

{
"code" : 0,
"message" : "",
"requestId":"14534664555",
"msgId":"123345346"
}