Help me understand how. RPC works and its best use...
# ask-ai
e
Help me understand how. RPC works and its best use case be detailed and use list to explain
t
Thanks for reaching out, Brian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! I’d love to help you understand how Remote Procedure Calls (RPC) work in LiveKit. This feature lets participants in a room call specific methods on other participants and get responses in real-time. Let me walk you through the details: How RPC Works: • Registering an RPC Method: Each participant can set up a method using
registerRpcMethod
. You give it a method name and a handler function that will process incoming calls. The handler gets useful info like who called, the data sent, and how long to wait for a response. Here’s an example: `room.localParticipant?.registerRpcMethod('greet', async (data) => { console.log(
Received greeting from ${data.callerIdentity}: ${data.payload}
); return `Hello, ${data.callerIdentity}!
Copy code
;
});
• Performing an RPC Request: When you want to call a method on someone else, you use
performRpc
, specifying who you’re calling, the method name, and any data you want to send. Like this:
Copy code
const response = await room.localParticipant!.performRpc({
  destinationIdentity: 'recipient-identity',
  method: 'greet',
  payload: 'Hello from RPC!',
});
• If the participant you’re calling doesn’t respond in time, you’ll get an error — so it’s good to keep an eye on those timeouts. Best Use Cases: • Forwarding tool calls from AI agents to frontend apps: For example, an AI can trigger a function on the frontend, like asking for the user’s location through browser APIs. • Keeping app states synchronized in real-time between participants. • Requesting data from peers exactly when you need it. • Building fun interactive features like greetings, games, or collaborative tools. And the great news is, RPC works across various SDKs — JavaScript, Python, Unity, and Node.js — so you can easily connect participants on different platforms. Sources: packages/livekit-rtc/README.md | Tool definition and use | LiveKit docs | README.md | README.md | Remote method calls | LiveKit docs