to use the rpc, what should I define, import and u...
# ask-ai
s
to use the rpc, what should I define, import and use in livekit agent?
t
Thanks for reaching out, Takeshi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To use the RPC feature in a LiveKit agent, you need to: • Import the required SDK packages for your language: ◦ Node.js:
@livekit/rtc-node
and
livekit-server-sdk
◦ Python:
livekit
package • Register RPC methods your agent will handle. For example: ◦ Node.js: `room.localParticipant?.registerRpcMethod( 'greet', async (data) => { console.log(
Received greeting from ${data.callerIdentity}: ${data.payload}
); return `Hello, ${data.callerIdentity}!
Copy code
;
  }
);
◦ Python:
Copy code
@room.local_participant.register_rpc_method("greet")
async def handle_greet(data):
    print(f"Received greeting from {data.caller_identity}: {data.payload}")
    return f"Hello, {data.caller_identity}!"
• Invoke RPC methods on other participants: ◦ Node.js:
Copy code
const response = await room.localParticipant!.performRpc({
  destinationIdentity: 'recipient-identity',
  method: 'greet',
  payload: 'Hello from RPC!',
});
◦ Python:
Copy code
response = await room.local_participant.perform_rpc(
  destination_identity='recipient-identity',
  method='greet',
  payload='Hello from RPC!'
)
• Work with RPC data types such as
RpcInvocationData
(for handler input),
PerformRpcRequest
(for making calls), and
RegisterRpcMethodRequest
(for registration). • Handle errors by throwing
RpcError
in your handler if needed. Other errors will be sent as "Application Error". The essential pattern is: 1. Connect to the LiveKit room 2. Register your RPC methods 3. Handle incoming invocations in your handler 4. Call RPC methods on other participants as needed This enables powerful agent-to-agent or agent-to-client communication using custom RPC methods. Sources: packages/livekit-rtc/README.md | livekit-rtc/livekit/rtc/_proto/rpc_pb2.py | Remote method calls (RPC) | LiveKit Docs | Web and mobile frontends | LiveKit Docs
s
where to register RPC methods? Where to handle invocations?
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!