|
| 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++! |
0 commit comments