1. StreamNative Cloud
  2. Connect

Connect to cluster through C++ client

This document describes how to connect to a cluster through a C++ client, and use the C++ producer and consumer to produce and consume messages to and from a topic. The Java client supports to connect to a Pulsar cluster either through the Oauth2 authentication plugin or Token authentication plugin.

This document assumes that you have created a Pulsar cluster and a service account, and have granted the service account produce and consume permissions to the namespace for the target topic.

Connect to cluster through OAuth2 authentication plugin

To connect a cluster through the OAuth2 authentication plugin, follow these steps.

  1. Get the service URL of your Pulsar cluster. For details, see get a service URL.

  2. Get the OAuth2 credential file of your service account. For details, see get an OAuth2 credential file.

  3. Connect to a Pulsar cluster through the OAuth2 authentication plugin.

    int main() {
      ClientConfiguration config;
      std::string oauthParams = R"({
      "issuer_url": "https://auth.streamnative.cloud",
      "private_key": "file:///absolute path/to/key/file.json",
      "audience": "https://cloud.auth0.com/api/v2/"})";
    
      config.setAuth(pulsar::AuthOauth2::create(oauthParams));
    
      Client client("broker-service-url", config);
      client.close();
    }
    
    • issuerUrl: the URL of your OAuth2 authentication provider. You can get the value from your downloaded OAuth2 credential file.
    • private_key: the path to your downloaded OAuth2 credential file. The privateKey parameter supports the following three pattern formats:
      • file:///path/to/file
      • file:/path/to/file
      • data:application/json;base64,<base64-encoded value>
    • audience: the audience parameter is the Uniform Resource Name (URN), which is a combination of the urn:sn:pulsar, the organization name, and the Pulsar instance name, in this format urn:sn:pulsar:<org_name>:<instance_name>.
    • broker-service-url: the broker service URL of your Pulsar cluster.
  4. Create a C++ consumer and use the C++ consumer to consume messages.

    int main(int argc, char *argv[]) {
      ClientConfiguration config;
      std::string oauthParams = argv[2];
    
      config.setAuth(pulsar::AuthOauth2::create(oauthParams));
    
      Client client(argv[1], config);
    
      Consumer consumer;
      Result result = client.subscribe("persistent://public/default/my-topic", "consumer-1", consumer);
      if (result != ResultOk) {
        std::cout << "Failed to subscribe: " << result << "\n";
        return -1;
      }
    
      Message msg;
      while (true) {
        consumer.receive(msg);
        std::cout << "Received: " << msg << "  with payload '" << msg.getDataAsString() << "'" << "\n";
    
        consumer.acknowledge(msg);
      }
    }
    
  5. Create a C++ producer and use the C++ producer to produce messages.

    int main(int argc, char *argv[]) {
      ClientConfiguration config;
      std::string oauthParams = argv[2];
    
      config.setAuth(pulsar::AuthOauth2::create(oauthParams));
    
      Client client(argv[1], config);
    
      Producer producer;
      Result result = client.createProducer("persistent://public/default/my-topic", producer);
      if (result != ResultOk) {
        std::cout << "Error creating producer: " << result << "\n";
        return -1;
      }
    
      // Send synchronously
      Message msg = MessageBuilder().setContent("content").build();
      Result res = producer.send(msg);
      std::cout << "Message sent: " << res << "\n";
    
      client.close();
    }
    

Connect to cluster through Token authentication plugin

To connect a cluster through the Token authentication plugin, follow these steps.

  1. Get the service URL of your Pulsar cluster. For details, see get a service URL.

  2. Get the token of your service account. For details, see get a token.

  3. Connect to a Pulsar cluster through the Token authentication plugin.

    int main() {
      ClientConfiguration config;
      config.setAuth(AuthToken::create("AUTH_PARAMS"));
      Client client(SERVICE_URL, config);
    
      client.close();
    }
    
    • SERVICE_URL: the broker service URL of your Pulsar cluster.
    • AUTH_PARAMS: the token of your service account.

For a complete example about how to connect to a cluster through the Pulsar C++ client, see C++ client examples.

Previous
Clients - Java