C client

Obtaining the C client library

The Pulsar C client API and library is included with the same C++ libpulsar. Follow the instructions on C++ client.

Reference documentation

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

Producer Example

producer.c
#include <pulsar/c/client.h>
#include <stdio.h>
#include <string.h>
int main() {
pulsar_client_configuration_t *conf = pulsar_client_configuration_create();
pulsar_client_configuration_set_auth(conf, pulsar_authentication_token_create(
"s.JzdWIiOiJKb2UifQ")
pulsar_client_t *client = pulsar_client_create(
"pulsar+ssl://prod.my-company.cloud.streamlio.com:6651", conf);
pulsar_producer_configuration_t* producer_conf = pulsar_producer_configuration_create();
pulsar_producer_configuration_set_batching_enabled(producer_conf, 1);
pulsar_producer_t *producer;
pulsar_result err = pulsar_client_create_producer(client,
"my-tenant/my-namespace/my-topic",
producer_conf, &producer);
if (err != pulsar_result_Ok) {
printf("Failed to create producer: %s\n", pulsar_result_str(err));
return 1;
}
const char* data = "Hello";
pulsar_message_t* message = pulsar_message_create();
pulsar_message_set_content(message, data, strlen(data));
err = pulsar_producer_send(producer, message);
if (err == pulsar_result_Ok) {
printf("Sent message %d\n", i);
} else {
printf("Failed to publish message: %s\n", pulsar_result_str(err));
}
pulsar_message_free(message);
// Cleanup
pulsar_producer_close(producer);
pulsar_producer_free(producer);
pulsar_producer_configuration_free(producer_conf);
pulsar_client_close(client);
pulsar_client_free(client);
pulsar_client_configuration_free(conf);
}

You can compile it with:

gcc producer.c -o producer -lpulsar

Consumer Example

consumer.c
#include <stdio.h>
#include <pulsar/c/client.h>
int main() {
pulsar_client_configuration_t *conf = pulsar_client_configuration_create();
pulsar_client_configuration_set_auth(conf, pulsar_authentication_token_create(
"s.JzdWIiOiJKb2UifQ")
pulsar_client_t *client = pulsar_client_create(
"pulsar+ssl://prod.my-company.cloud.streamlio.com:6651", conf);
pulsar_consumer_configuration_t *consumer_conf = pulsar_consumer_configuration_create();
pulsar_consumer_configuration_set_consumer_type(consumer_conf,
pulsar_ConsumerShared);
pulsar_consumer_t *consumer;
pulsar_result res = pulsar_client_subscribe(client,
"my-tenant/my-namespace/my-topic",
"my-subscrition",
consumer_conf, &consumer);
if (res != pulsar_result_Ok) {
printf("Failed to create subscribe to topic: %s\n", pulsar_result_str(res));
return 1;
}
for (;;) {
pulsar_message_t *message;
res = pulsar_consumer_receive(consumer, &message);
if (res != pulsar_result_Ok) {
printf("Failed to receive message: %s\n", pulsar_result_str(res));
return 1;
}
printf("Received message with payload: '%.*s'\n",
pulsar_message_get_length(message),
pulsar_message_get_data(message));
pulsar_consumer_acknowledge(consumer, message);
pulsar_message_free(message);
}
// Cleanup
pulsar_consumer_close(consumer);
pulsar_consumer_free(consumer);
pulsar_consumer_configuration_free(consumer_conf);
pulsar_client_close(client);
pulsar_client_free(client);
pulsar_client_configuration_free(conf);
}

You can compile it with:

gcc consumer.c -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.