tencent cloud

Last updated:2025-12-24 15:07:56
C++ SDK
Last updated: 2025-12-24 15:07:56

Scenarios

This document describes how to use an open-source SDK (taking the C++ SDK as an example) to send and receive messages, so as to help you better understand the complete process of sending and receiving messages.

Prerequisites

You have obtained the client connection parameters as instructed in SDK Overview.

Operation Steps

1. Prepare the environment.
1.1 Install the Pulsar C++ client in the client environment. For the installation process, see the official tutorial Pulsar C++ client.
1.2 Introduce the header files and dynamic libraries that are related to the Pulsar C++ client into the project.
2. Create a client.
// The client configuration information.
ClientConfiguration config;
// Set the authorized role token.
AuthenticationPtr auth = pulsar::AuthToken::createWithToken(AUTHENTICATION);
config.setAuth(auth);
// Create a client.
Client client(SERVICE_URL, config);
Parameter
Description
SERVICE_URL
Address used to access the cluster. You can view and copy the address from the Cluster page in the console.
AUTHENTICATION
Role token. You can copy the token from the Role Management page.
3. Create a producer.
// The producer configuration.
ProducerConfiguration producerConf;
producerConf.setBlockIfQueueFull(true);
producerConf.setSendTimeout(5000);
// The producer.
Producer producer;
// Create a producer.
Result result = client.createProducer(
// The full topic path in the format of persistent://cluster (tenant) ID/namespace/topic.
"persistent://pulsar-xxx/sdk_cpp/topic1",
producerConf,
producer);
if (result != ResultOk) {
std::cout << "Error creating producer: " << result << std::endl;
return -1;
}
Note
You need to provide the full path of the topic in the format of persistent://clusterid/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console.
3. Send a message.
// The message content.
std::string content = "hello cpp client, this is a msg";
// Construct a message object.
Message msg = MessageBuilder().setContent(content)
.setPartitionKey("mykey") // The business key.
.setProperty("x", "1") // Set message parameters.
.build();
// Send a message.
Result result = producer.send(msg);
if (result != ResultOk) {
// Sending fails.
std::cout << "The message " << content << " could not be sent, received code: " << result << std::endl;
} else {
// Sending succeeded.
std::cout << "The message " << content << " sent successfully" << std::endl;
}
4. Create a consumer.
// The consumer configuration information.
ConsumerConfiguration consumerConfiguration;
consumerConfiguration.setSubscriptionInitialPosition(pulsar::InitialPositionEarliest);
// The consumer.
Consumer consumer;
// Subscribe to a topic.
Result result = client.subscribe(
// The full topic path in the format of persistent://cluster (tenant) ID/namespace/topic.
"persistent://pulsar-xxx/sdk_cpp/topic1",
// The subscription name.
"sub_topic1",
consumerConfiguration,
consumer);

if (result != ResultOk) {
std::cout << "Failed to subscribe: " << result << std::endl;
return -1;
}

Note
You need to provide the full path of the topic in the format of persistent://clusterid/namespace/topic. You can copy the clusterid/namespace/topic part from the Topic page in the console. 
You need to enter the subscription name. You can view the subscription name on the Consumer tab of the topic.
5. Consume messages.
Message msg;
// Obtain messages.
consumer.receive(msg);
// Simulate the business logic.
std::cout << "Received: " << msg << " with payload '" << msg.getDataAsString() << "'" << std::endl;
// Reply ack.
consumer.acknowledge(msg);
// If the consumption fails, reply nack and the message will be redelivered.
// consumer.negativeAcknowledge(msg);
6. Log in to the TDMQ for Apache Pulsar console. On the Topic page, click the topic name to go to the Consumption Management page. Click the dropdown arrow next to the subscription name to view production and consumption records.
img


Note
The above is a brief introduction to the publishing and subscription of messages. For more information, see Demo or Pulsar Official Documentation.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback