Skip to content

Commit

Permalink
Added the first C example: pull consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
levb committed May 11, 2024
1 parent f869165 commit 9ece99e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist
node_modules
html
.idea/
.vscode/
**/node/node_modules/
**/node/package.json
**/node/package-lock.json
1 change: 1 addition & 0 deletions cmd/nbe/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (r *ImageBuilder) Run() (string, error) {
c := exec.Command(
"docker",
"build",
// "--progress=plain", // Use plain output for debuggig
"--tag", imageTag,
buildDir,
)
Expand Down
23 changes: 23 additions & 0 deletions docker/c/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM ubuntu:latest AS build

WORKDIR /opt/app

RUN apt-get update
RUN apt-get install -y build-essential
RUN apt-get install -y cmake
RUN apt-get install -y curl
RUN apt-get install -y git
RUN apt-get install -y golang
RUN apt-get install -y jq
RUN apt-get install -y libprotobuf-c-dev
RUN apt-get install -y libsodium-dev
RUN apt-get install -y libssl-dev
RUN apt-get install -y wget

COPY install-nats-c.sh .
RUN bash install-nats-c.sh

COPY . ./
RUN mkdir build && cd build && cmake .. && make

CMD ["/opt/app/build/app"]
15 changes: 15 additions & 0 deletions docker/c/install-nats-c.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
rel=$(curl -s https://api.github.com/repos/nats-io/nats.c/releases/latest | jq -r '.tag_name')
wget https://github.com/nats-io/nats.c/archive/refs/tags/${rel}.tar.gz
tar -xzf ${rel}.tar.gz
cd nats.c-${rel#v}
mkdir build
cd build
cmake \
-DNATS_BUILD_TLS_USE_OPENSSL_1_1_API=ON \
-DNATS_BUILD_STREAMING=OFF \
-DNATS_BUILD_EXAMPLES=OFF \
..
make
make install


8 changes: 8 additions & 0 deletions examples/jetstream/pull-consumer/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.28)

set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH "/usr/local/lib")

file(GLOB SOURCES "*.c")
add_executable(app ${SOURCES})
target_link_libraries(app nats)
79 changes: 79 additions & 0 deletions examples/jetstream/pull-consumer/c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <stdio.h>
#include <nats/nats.h>

int main()
{
natsStatus s = NATS_OK;
natsOptions *opts = NULL;
natsConnection *nc = NULL;
natsSubscription *sub = NULL;
natsMsg *msg = NULL;
jsOptions jsOpts;
jsCtx *js = NULL;
jsStreamConfig cfg;
jsSubOptions so;
jsStreamInfo *si = NULL;
jsErrCode jerr = 0;
natsMsgList list = {0};
int ifetch, iack;

const char *url = getenv("NATS_URL");
s = natsOptions_Create(&opts);
if (s == NATS_OK && url != NULL)
{
s = natsOptions_SetURL(opts, url);
}

if (s == NATS_OK)
s = natsConnection_Connect(&nc, opts);
if (s == NATS_OK)
s = jsOptions_Init(&jsOpts);
if (s == NATS_OK)
s = natsConnection_JetStream(&js, nc, &jsOpts);

if (s == NATS_OK)
s = jsStreamConfig_Init(&cfg);
if (s == NATS_OK)
{
cfg.Name = "EVENTS";
cfg.Subjects = (const char *[1]){"event.>"};
cfg.SubjectsLen = 1;
s = js_AddStream(&si, js, &cfg, &jsOpts, &jerr);
}

if (s == NATS_OK)
s = js_Publish(NULL, js, "event.1", NULL, 0, NULL, &jerr);
if (s == NATS_OK)
s = js_Publish(NULL, js, "event.2", NULL, 0, NULL, &jerr);
if (s == NATS_OK)
s = js_Publish(NULL, js, "event.3", NULL, 0, NULL, &jerr);

if (s == NATS_OK)
s = jsSubOptions_Init(&so);
if (s == NATS_OK)
{
so.Stream = "EVENTS";
s = js_PullSubscribe(&sub, js, "event.>", NULL, &jsOpts, &so, &jerr);
}

for (ifetch = 0; (s == NATS_OK) && (ifetch < 3);)
{
s = natsSubscription_Fetch(&list, sub, 10, 5000, &jerr);
if (s == NATS_OK)
ifetch += (int64_t)list.Count;
for (iack = 0; (s == NATS_OK) && (iack < list.Count); iack++)
{
s = natsMsg_Ack(list.Msgs[iack], &jsOpts);
if (s == NATS_OK)
printf("received message on %s\n", natsMsg_GetSubject(list.Msgs[iack]));
}
natsMsgList_Destroy(&list);
}

natsSubscription_Destroy(sub);
jsCtx_Destroy(js);
natsConnection_Destroy(nc);
natsOptions_Destroy(opts);

return 0;
}

0 comments on commit 9ece99e

Please sign in to comment.