|
| 1 | +# 📘 SimpleRpc Example — Technical Overview |
| 2 | + |
| 3 | +This README provides deeper technical details about the RPC (Remote Procedure Call) support demonstrated in the SimpleRpc example. |
| 4 | +It complements the example instructions found in the root README.md. |
| 5 | + |
| 6 | +If you're looking for how to run the example, see the root [README](https://github.com/livekit/client-sdk-cpp). |
| 7 | + |
| 8 | +This document explains: |
| 9 | +- How LiveKit RPC works in the C++ SDK |
| 10 | +- Where the APIs are defined |
| 11 | +- How senders call RPC methods |
| 12 | +- How receivers register handlers |
| 13 | +- What happens if the receiver is absent |
| 14 | +- How long-running operations behave |
| 15 | +- Timeouts, disconnects, and unsupported methods |
| 16 | +- RPC lifecycle events and error propagation |
| 17 | + |
| 18 | +## 🔧 Overview: How RPC Works |
| 19 | +LiveKit RPC allows one participant (the caller) to invoke a method on another participant (the receiver) using the data channel transport. |
| 20 | +It is: |
| 21 | +- Peer-to-peer within the room (not server-executed RPC) |
| 22 | +- Request/response (caller waits for a reply or an error) |
| 23 | +- Asynchronous under the hood, synchronous or blocking from the caller’s perspective |
| 24 | +- Delivery-guaranteed when using the reliable data channel |
| 25 | + |
| 26 | +Each RPC call includes: |
| 27 | +| Field | Meaning | |
| 28 | +|--------------------------|-----------------------------------------------------| |
| 29 | +| **destination_identity** | Identity of the target participant | |
| 30 | +| **method** | Method name string (e.g., "square-root") | |
| 31 | +| **payload** | Arbitrary UTF-8 text | |
| 32 | +| **response_timeout** | Optional timeout (seconds) | |
| 33 | +| **invocation_id** | Server-generated ID used internally for correlation | |
| 34 | + |
| 35 | +## 📍 Location of APIs in C++ |
| 36 | +All public-facing RPC APIs live in: |
| 37 | +[include/livekit/local_participant.h](https://github.com/livekit/client-sdk-cpp/blob/main/include/livekit/local_participant.h#L160) |
| 38 | + |
| 39 | +### Key methods: |
| 40 | + |
| 41 | +#### Sender-side APIs |
| 42 | +```bash |
| 43 | +std::string performRpc( |
| 44 | + const std::string& destination_identity, |
| 45 | + const std::string& method, |
| 46 | + const std::string& payload, |
| 47 | + std::optional<double> response_timeout_sec = std::nullopt |
| 48 | +); |
| 49 | + |
| 50 | +Receiver-side APIs |
| 51 | +void registerRpcMethod( |
| 52 | + const std::string& method_name, |
| 53 | + RpcHandler handler |
| 54 | +); |
| 55 | + |
| 56 | +void unregisterRpcMethod(const std::string& method_name); |
| 57 | + |
| 58 | +Handler signature |
| 59 | +using RpcHandler = |
| 60 | + std::function<std::optional<std::string>(const RpcInvocationData&)>; |
| 61 | +``` |
| 62 | + |
| 63 | +Handlers can: |
| 64 | +- Return a string (the RPC response payload) |
| 65 | +- Return std::nullopt (meaning “no return payload”) |
| 66 | +- Throw exceptions (mapped to APPLICATION_ERROR) |
| 67 | +- Throw a RpcError (mapped to specific error codes) |
| 68 | + |
| 69 | +#### 🛰 Sender Behavior (performRpc) |
| 70 | + |
| 71 | +When the caller invokes: |
| 72 | +```bash |
| 73 | +auto reply = lp->performRpc("math-genius", "square-root", "{\"number\":16}"); |
| 74 | +``` |
| 75 | + |
| 76 | +The following occurs: |
| 77 | + |
| 78 | +A PerformRpcRequest is sent through FFI to the SDK core. |
| 79 | + |
| 80 | +The SDK transmits the invocation to the target participant (if present). |
| 81 | + |
| 82 | +The caller begins waiting for a matching RpcMethodInvocationResponse. |
| 83 | + |
| 84 | +One of the following happens: |
| 85 | +| Outcome | Meaning | |
| 86 | +|--------------------------|------------------------------------------| |
| 87 | +| **Success** | Receiver returned a payload | |
| 88 | +| **UNSUPPORTED_METHOD** | Receiver did not register the method | |
| 89 | +| **RECIPIENT_NOT_FOUND** | Target identity not present in room | |
| 90 | +| **RECIPIENT_DISCONNECTED** | Target left before replying | |
| 91 | +| **RESPONSE_TIMEOUT** | Receiver took too long | |
| 92 | +| **APPLICATION_ERROR** | Handler threw an exception | |
| 93 | + |
| 94 | +#### 🔄 Round-trip time (RTT) |
| 95 | + |
| 96 | +The caller can measure RTT externally (as SimpleRpc does), but the SDK does not measure RTT internally. |
| 97 | + |
| 98 | +#### 📡 Receiver Behavior (registerRpcMethod) |
| 99 | + |
| 100 | +A receiver must explicitly register handlers: |
| 101 | +```bash |
| 102 | +local_participant->registerRpcMethod("square-root", |
| 103 | + [](const RpcInvocationData& data) { |
| 104 | + double number = parse(data.payload); |
| 105 | + return make_json("result", std::sqrt(number)); |
| 106 | + }); |
| 107 | +``` |
| 108 | + |
| 109 | +When an invocation arrives: |
| 110 | +- Room receives a RpcMethodInvocationEvent |
| 111 | +- Room forwards it to the corresponding LocalParticipant |
| 112 | +- LocalParticipant::handleRpcMethodInvocation(): |
| 113 | +- Calls the handler |
| 114 | +- Converts any exceptions into RpcError |
| 115 | +- Sends back RpcMethodInvocationResponse |
| 116 | + |
| 117 | +⚠ If no handler exists: |
| 118 | + |
| 119 | +Receiver returns: UNSUPPORTED_METHOD |
| 120 | + |
| 121 | + |
| 122 | +#### 🚨 What Happens if Receiver Is Absent? |
| 123 | +| Case | Behavior | |
| 124 | +|-----------------------------------------------------|---------------------------------------------------| |
| 125 | +| Receiver identity is not in the room | Caller immediately receives `RECIPIENT_NOT_FOUND` | |
| 126 | +| Receiver is present but disconnects before replying | Caller receives `RECIPIENT_DISCONNECTED` | |
| 127 | +| Receiver joins later | Caller must retry manually (no automatic waiting) | |
| 128 | + |
| 129 | +**Important**: |
| 130 | +LiveKit does not queue RPC calls for offline participants. |
| 131 | + |
| 132 | +#### ⏳ Timeout Behavior |
| 133 | + |
| 134 | +If the caller specifies: |
| 135 | + |
| 136 | +performRpc(..., /*response_timeout=*/10.0); |
| 137 | + |
| 138 | +Then: |
| 139 | +- Receiver is given 10 seconds to respond. |
| 140 | +- If the receiver handler takes longer (e.g., sleep 30s), caller receives: |
| 141 | +RESPONSE_TIMEOUT |
| 142 | + |
| 143 | +**If no response_timeout is provided explicitly, the default timeout is 15 seconds.** |
| 144 | + |
| 145 | + |
| 146 | +This is by design and demonstrated in the example. |
| 147 | + |
| 148 | +#### 🧨 Errors & Failure Modes |
| 149 | +| Error Code | Cause | |
| 150 | +|------------------------|---------------------------------------------| |
| 151 | +| **APPLICATION_ERROR** | Handler threw a C++ exception | |
| 152 | +| **UNSUPPORTED_METHOD** | No handler registered for the method | |
| 153 | +| **RECIPIENT_NOT_FOUND** | Destination identity not in room | |
| 154 | +| **RECIPIENT_DISCONNECTED** | Participant left mid-flight | |
| 155 | +| **RESPONSE_TIMEOUT** | Handler exceeded allowed response time | |
| 156 | +| **CONNECTION_TIMEOUT** | Transport-level issue | |
| 157 | +| **SEND_FAILED** | SDK failed to send invocation | |
0 commit comments