-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the first C example: pull consumer
- Loading branch information
Showing
6 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ dist | |
node_modules | ||
html | ||
.idea/ | ||
.vscode/ | ||
**/node/node_modules/ | ||
**/node/package.json | ||
**/node/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |