The Pulsar C++ client

    Linux

    RPM

    To install a RPM package, download the RPM packages and install them using the following command:

    DEB

    To install a DEB package, download the DEB packages and install them using the following command:

    Build

    If you want to build RPM and Debian packages off latest master, you can follow the instructionsbelow to do so. All the instructions are run at the root directory of your cloned Pulsarrepo.

    To build the C++ library packages, first build the Java packages:

    1. mvn install -DskipTests

    RPM

    1. pulsar-client-cpp/pkg/rpm/docker-build-rpm.sh

    This will build the RPM inside a Docker container and it will leave the RPMsin pulsar-client-cpp/pkg/rpm/RPMS/x86_64/.

    Deb

    To build Debian packages:

    Debian packages will be created at pulsar-client-cpp/pkg/deb/BUILD/DEB/

    1. brew install libpulsar

    This will install the package with the library and headers.

    Connection URLs

    To connect to Pulsar using client libraries, you need to specify a Pulsar protocol URL.

    Pulsar protocol URLs are assigned to specific clusters, use the pulsar URI scheme and have a default port of 6650. Here’s an example for localhost:

    1. pulsar://localhost:6650

    A URL for a production Pulsar cluster may look something like this:

    1. pulsar://pulsar.us-west.example.com:6650
    1. Client client("pulsar://localhost:6650");
    2. Consumer consumer;
    3. Result result = client.subscribe("my-topic", "my-subscribtion-name", consumer);
    4. LOG_ERROR("Failed to subscribe: " << result);
    5. return -1;
    6. }
    7. Message msg;
    8. while (true) {
    9. consumer.receive(msg);
    10. LOG_INFO("Received: " << msg
    11. << " with payload '" << msg.getDataAsString() << "'");
    12. consumer.acknowledge(msg);
    13. }
    14. client.close();

    Producer

    1. Client client("pulsar://localhost:6650");
    2. if (result != ResultOk) {
    3. LOG_ERROR("Error creating producer: " << result);
    4. return -1;
    5. }
    6. // Publish 10 messages to the topic
    7. for (int i = 0; i < 10; i++){
    8. Message msg = MessageBuilder().setContent("my-message").build();
    9. Result res = producer.send(msg);
    10. LOG_INFO("Message sent: " << res);
    11. }
    12. client.close();
    1. ClientConfiguration config = ClientConfiguration();
    2. config.setUseTls(true);
    3. config.setTlsTrustCertsFilePath("/path/to/cacert.pem");
    4. config.setTlsAllowInsecureConnection(false);
    5. config.setAuth(pulsar::AuthTls::create(
    6. "/path/to/client-cert.pem", "/path/to/client-key.pem"););