Skip to content

Commit 2235dda

Browse files
[feature] Add small example of current state
1 parent 3cc6313 commit 2235dda

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

examples/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(fluvio_examples CXX)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
# VCPKG dependencies
7+
find_package(nlohmann_json CONFIG REQUIRED)
8+
find_package(fmt CONFIG REQUIRED)
9+
10+
# The downloaded release via FetchContent
11+
include(FetchContent)
12+
FetchContent_Declare(
13+
fluvio_client_cpp
14+
URL https://github.com/stefanDeveloper/fluvio-client-cpp/releases/download/v0.0.2/fluvio-client-cpp-linux-x64.tar.gz
15+
)
16+
FetchContent_MakeAvailable(fluvio_client_cpp)
17+
18+
find_package(fluvio_client_cpp CONFIG REQUIRED PATHS ${fluvio_client_cpp_SOURCE_DIR})
19+
20+
add_executable(producer_example producer.cpp)
21+
target_link_libraries(producer_example PRIVATE nlohmann_json::nlohmann_json fmt::fmt fluvio_client_cpp::fluvio_client_cpp)
22+
23+
add_executable(consumer_example consumer.cpp)
24+
target_link_libraries(consumer_example PRIVATE nlohmann_json::nlohmann_json fmt::fmt fluvio_client_cpp::fluvio_client_cpp)

examples/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Fluvio C++ Client Examples
2+
3+
Welcome to the Fluvio C++ Client examples! These examples provide a quick and easy way to learn how to write native C++ applications that interact with a Fluvio streaming cluster.
4+
5+
## Prerequisites
6+
7+
Before building the examples, ensure you have the following:
8+
- A modern C++ compiler (supporting C++17 or later)
9+
- [CMake](https://cmake.org/download/) (version 3.20+)
10+
- [vcpkg](https://vcpkg.io/en/getting-started.html) for dependency management
11+
- A running [Fluvio cluster](https://www.fluvio.io/docs/get-started/) (local or InfinyOn Cloud)
12+
13+
## What's Included?
14+
15+
We provide two simple applications to demonstrate the core features of the client:
16+
17+
- **Producer (`producer.cpp`)**: Connects to the Fluvio cluster as an admin to ensure a topic named `example-topic` exists. It then creates a producer and sends a mock JSON payload representing sensor data.
18+
- **Consumer (`consumer.cpp`)**: Connects to the Fluvio cluster, opens a stream on `example-topic`, and parses the incoming JSON data using `nlohmann::json`.
19+
20+
## Building the Examples
21+
22+
The examples use `vcpkg` to pull in third-party libraries like `fmt` for modern logging and `nlohmann-json` for JSON parsing. The Fluvio C++ library itself is downloaded automatically via CMake's `FetchContent` using the prebuilt GitHub release binaries.
23+
24+
1. Make sure you have the `VCPKG_ROOT` environment variable set to your vcpkg installation path:
25+
```bash
26+
export VCPKG_ROOT=/path/to/your/vcpkg
27+
```
28+
29+
2. Configure the CMake project:
30+
```bash
31+
cmake -S . -B build -G Ninja \
32+
-DCMAKE_BUILD_TYPE=Release \
33+
-DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
34+
```
35+
*(Note: You can omit `-G Ninja` if you haven't installed `ninja` and prefer the default Makefile generator).*
36+
37+
3. Build the executables:
38+
```bash
39+
cmake --build build --parallel
40+
```
41+
42+
## Running the Examples
43+
44+
Make sure your Fluvio cluster is running and your current environment has access to it.
45+
46+
1. Start the consumer. It will connect to the cluster and wait for messages:
47+
```bash
48+
./build/consumer_example
49+
```
50+
51+
2. Open a second terminal window/tab and run the producer:
52+
```bash
53+
./build/producer_example
54+
```
55+
56+
**Expected Output:**
57+
58+
The producer will output:
59+
```text
60+
Starting Fluvio Producer Example...
61+
Created 'example-topic'.
62+
Sending JSON: {"sensor":"temp-01","status":"active","value":24.5}
63+
Record successfully sent to Fluvio!
64+
```
65+
66+
The consumer will receive the data, parse it, and output:
67+
```text
68+
Starting Fluvio Consumer Example...
69+
Waiting for messages...
70+
Received Raw Bytes: {"sensor":"temp-01","status":"active","value":24.5}
71+
Parsed JSON successfully: Sensor=temp-01 Value=24.5
72+
```
73+
74+
Congratulations! You've successfully streamed data using C++!

examples/consumer.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "fluvio-client-cpp/src/lib.rs.h"
2+
#include <iostream>
3+
#include <fmt/core.h>
4+
#include <nlohmann/json.hpp>
5+
6+
using json = nlohmann::json;
7+
8+
int main() {
9+
try {
10+
fmt::print("Starting Fluvio Consumer Example...\n");
11+
12+
auto client = fluvio_connect();
13+
auto consumer = partition_consumer(*client, "example-topic", 0);
14+
15+
auto stream = consumer_stream(*consumer, 0); // Offset::beginning()
16+
17+
fmt::print("Waiting for messages...\n");
18+
19+
// Fetch one record
20+
auto rec = stream_next(*stream);
21+
auto val = record_value(*rec);
22+
23+
std::string payload(val.begin(), val.end());
24+
fmt::print("Received Raw Bytes: {}\n", payload);
25+
26+
try {
27+
json j = json::parse(payload);
28+
fmt::print("Parsed JSON successfully: Sensor={} Value={}\n",
29+
j["sensor"].get<std::string>(),
30+
j["value"].get<double>());
31+
} catch (const json::parse_error& e) {
32+
fmt::print(stderr, "Failed to parse JSON: {}\n", e.what());
33+
}
34+
35+
} catch (const std::exception& e) {
36+
fmt::print(stderr, "Fatal error: {}\n", e.what());
37+
return 1;
38+
}
39+
return 0;
40+
}

examples/producer.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "fluvio-client-cpp/src/lib.rs.h"
2+
#include <iostream>
3+
#include <fmt/core.h>
4+
#include <nlohmann/json.hpp>
5+
6+
using json = nlohmann::json;
7+
8+
int main() {
9+
try {
10+
fmt::print("Starting Fluvio Producer Example...\n");
11+
12+
auto admin = fluvio_admin_connect();
13+
try {
14+
admin_create_topic(*admin, "example-topic", 1, 1);
15+
fmt::print("Created 'example-topic'.\n");
16+
} catch (...) {
17+
fmt::print("'example-topic' already exists or creation failed.\n");
18+
}
19+
20+
auto client = fluvio_connect();
21+
auto producer = create_producer(*client, "example-topic");
22+
23+
// Create a JSON payload
24+
json j = {
25+
{"sensor", "temp-01"},
26+
{"value", 24.5},
27+
{"status", "active"}
28+
};
29+
std::string payload = j.dump();
30+
31+
fmt::print("Sending JSON: {}\n", payload);
32+
33+
uint8_t key[] = {'j', 's', 'o', 'n'};
34+
auto out = producer_send(*producer,
35+
rust::Slice<const uint8_t>(key, sizeof(key)),
36+
rust::Slice<const uint8_t>(reinterpret_cast<const uint8_t*>(payload.data()), payload.size())
37+
);
38+
39+
auto meta = produce_output_wait(*out);
40+
producer_flush(*producer);
41+
42+
fmt::print("Record successfully sent to Fluvio!\n");
43+
44+
} catch (const std::exception& e) {
45+
fmt::print(stderr, "Fatal error: {}\n", e.what());
46+
return 1;
47+
}
48+
return 0;
49+
}

examples/vcpkg.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "fluvio-examples",
3+
"version-string": "0.0.2",
4+
"dependencies": [
5+
"nlohmann-json",
6+
"fmt"
7+
]
8+
}

0 commit comments

Comments
 (0)