C++ client

Obtaining the C++ client library

Apache Pulsar C++ client library is distributed in binary form the official Apache Pulsar releases page.

Linux - Debian / Ubuntu

Download the Deb packages from https://pulsar.apache.org/docs/en/client-libraries-cpp/#deb.

You will have:

  • apache-pulsar-client.deb

  • apache-pulsar-client-dev.deb

You can now install the packages:

apt install ./apache-pulsar-client*.deb

Linux - RedHat / CentOS

Download the RPM binary packages from https://pulsar.apache.org/docs/en/client-libraries-cpp/#rpm.

You will have:

  • apache-pulsar-client-$VERSION.x86_64.rpm

  • apache-pulsar-client-devel-$VERSION.x86_64.rpm

You can now install the packages:

rpm -ivh apache-pulsar-client*.rpm

MacOS

MacOS binaries for Pulsar client library are available from Homebrew-Core distribution. To install it, you can simply:

brew install libpulsar

Reference documentation

The Doxygen reference documentation for C++ is published at https://pulsar.apache.org/api/cpp.

Producer Example

producer.cpp
#include <pulsar/Client.h>
#include <iostream>
using namespace pulsar;
int main() {
ClientConfiguration config;
config.setAuth(AuthToken::createWithToken("s.JzdWIiOiJKb2UifQ"));
Client client("pulsar+ssl://prod.my-company.cloud.streamlio.com:6651");
Producer producer;
Result result = client.createProducer("my-tenant/my-namespace/my-topic", producer);
if (result != ResultOk) {
std::cerr << "Error creating producer: " << result << std::endl;
return -1;
}
Message msg = MessageBuilder().setContent("hello").build();
Result res = producer.send(msg);
std::cout << "Message sent: " << res << std::endl;
client.close();
}

You can compile it with:

g++ -std=c++11 producer.cpp -o producer -lpulsar

Consumer Example

consumer.cpp
#include <pulsar/Client.h>
#include <iostream>
using namespace pulsar;
int main() {
ClientConfiguration config;
config.setAuth(AuthToken::createWithToken("s.JzdWIiOiJKb2UifQ"));
Client client("pulsar+ssl://prod.my-company.cloud.streamlio.com:6651");
Consumer consumer;
Result result = client.subscribe("my-tenant/my-namespace/my-topic",
"my-subscribtion-name", consumer);
if (result != ResultOk) {
std::cerr << "Failed to subscribe: " << result << std::endl;
return -1;
}
Message msg;
while (true) {
consumer.receive(msg);
std::cout << "Received: " << msg
<< " with payload '" << msg.getDataAsString() << "'"
<< std::endl;
consumer.acknowledge(msg);
}
client.close();
}

You can compile it with:

g++ -std=c++11 consumer.cpp -o consumer -lpulsar

_____ Copyright 2019 Streamlio, Inc. Apache, Apache BookKeeper, Apache Pulsar and associated open source project names are trademarks of the Apache Software Foundation.