Skip to content
heisenwarePublic

About

Asynchronous RPC via MQTT for header-only C++

Topics

Resources

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Repository files navigation

VRPC C++ Agent

License: MIT C++14

Stop writing API boilerplate. VRPC (Virtual Remote Procedure Call) allows you to call C++, Node.js, Python, and Arduino classes across any network as if they were local objects. It is perfect for microservices, IoT edge devices, and directly driving React frontends—without the need for REST, GraphQL, or WebSocket boilerplate.

This repository provides the C++ Agent, allowing you to non-intrusively bind your existing C++ code and make it remotely controllable in minutes.


Why VRPC for C++?

  • Zero Boilerplate: No IDL files (like Protobuf or gRPC), no route definitions, and no payload parsing. Just use simple macros to register your class, and VRPC instantly makes its public methods remotely callable.
  • Native Event Proxies: Don't just fetch data—stream it. VRPC transparently proxies C++ callbacks across the network. When your C++ edge device emits a hardware event, your React UI or Node.js backend updates instantly.
  • MQTT-Powered NAT Traversal: Built on top of robust MQTT, VRPC agents make outbound connections to your broker. No open firewall ports, no complex reverse proxies, and perfect resilience on unstable edge networks.
  • Header-Only Core: The binding adapter is lightweight and highly template-driven, seamlessly handling variadic arguments and complex data types via JSON serialization.

Quick Start

With VRPC, making a C++ class remotely accessible requires almost zero changes to your actual business logic. As a modern header-only library, integrating it into your project is trivial.

1. Integrate via CMake

Add vrpc-hpp to your CMakeLists.txt using FetchContent. We safely bundle the necessary Boost dependencies by default (but will automatically use your system's Boost if you have one installed to prevent conflicts).

cmake_minimum_required(VERSION 3.14)
project(vrpc_example LANGUAGES CXX)

include(FetchContent)
FetchContent_Declare(
  vrpc
  GIT_REPOSITORY https://github.com/heisenware/vrpc-hpp.git
  GIT_TAG        master
)
FetchContent_MakeAvailable(vrpc)

add_executable(my_vrpc_agent main.cpp adapter.cpp)

# Link your target against vrpc::vrpc
# (This automatically configures include paths, Boost, and threading!)
target_link_libraries(my_vrpc_agent PRIVATE vrpc::vrpc)

2. Write your C++ Class

// Foo.hpp
#include <iostream>
#include <functional>

class Foo {
  int _value;

public:
  Foo(int initial_value) : _value(initial_value) {}

  int increment() {
    return ++_value;
  }

  // VRPC supports asynchronous callbacks out of the box!
  void onValue(const std::function<void(int)>& callback) {
     callback(_value);
  }
};

3. Bind it using VRPC

// adapter.cpp
#include <vrpc/adapter.hpp>
#include "Foo.hpp"

// Bind the constructor
VRPC_CTOR(Foo, int)

// Bind the member functions
VRPC_MEMBER_FUNCTION(Foo, int, increment)
VRPC_MEMBER_FUNCTION(Foo, void, onValue, VRPC_CALLBACK(int))

4. Build and Start the Agent (Secure by Default)

VRPC is designed for the modern web and uses TLS/SSL to connect securely to your broker (mqtts:// or ssl://). This requires OpenSSL.

1. Install OpenSSL:

  • Linux (Ubuntu/Debian): sudo apt-get install libssl-dev
  • Linux (Alpine): apk add openssl-dev
  • Windows (via vcpkg): vcpkg install openssl:x64-windows

2. Configure and Build: Tell CMake to enable TLS. (If on Windows, make sure to pass your vcpkg toolchain file so CMake finds OpenSSL).

mkdir build && cd build
cmake .. -DVRPC_WITH_TLS=ON
make

# Start the agent and connect securely!
./my_vrpc_agent -d my_domain -a cpp_edge_device -b mqtts://broker.hivemq.com:8883

(Note: You can also force VRPC to use its bundled Boost headers by passing -DVRPC_USE_BUNDLED_BOOST=ON during configuration).

5. Call it from Anywhere (e.g., Node.js / React)

Once your C++ agent is running, you can interact with it transparently from any VRPC client. Let's create a quick Node.js client!

1. Set up a new Node.js project: Create a new directory and install the VRPC Javascript package.

mkdir vrpc-client && cd vrpc-client
npm install vrpc

2. Create the client code (index.js):

const { VrpcClient } = require('vrpc')

async function main() {
  const client = new VrpcClient({ domain: 'my_domain' })

  await client.connect()

  // Create a remote instance of your C++ class
  const foo = await client.create({
    agent: 'cpp_edge_device',
    className: 'Foo',
    args: [41]
  })

  // Call functions natively
  const result = await foo.increment()
  console.log(result) // 42

  // Listen to C++ callbacks across the network!
  await foo.onValue(val => {
    console.log(`C++ emitted: ${val}`)
  })
}

main().catch(console.error)

3. Run the client:

node index.js

The VRPC Ecosystem

Write your performance-critical code in C++, your data-science scripts in Python, your business logic in Node.js, and your IoT firmware on Arduino. Call them all identically.

Documentation

For detailed installation instructions, CMake integration, advanced macro usage, and architecture overviews, please visit our official documentation at vrpc.io/docs.

Contributing

Contributions are welcome! Whether it's reporting a bug, proposing a new feature, or submitting a pull request, we'd love your help to make VRPC even better. Please read our Contributing Guidelines to get started.

License

VRPC is released under the MIT License.

About

Asynchronous RPC via MQTT for header-only C++

Topics

Resources

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages