Can i do rpc calls from agent backend to frontend?
# ask-ai
f
Can i do rpc calls from agent backend to frontend?
t
Thanks for reaching out, muse. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can perform RPC calls from the agent backend to the frontend in LiveKit! This is a great way to enable smooth, bidirectional communication, so your backend can update UI elements, fetch real-time data, or even trigger actions on the frontend while a session is ongoing. Here’s how you can set this up: 1. Register RPC Methods on the Frontend: You’ll want to use
registerRpcMethod
on the
localParticipant
to set up handlers for the methods the backend will call. For example:
Copy code
localParticipant.registerRpcMethod('updateField', async (data) => {
  const payload = JSON.parse(data.payload);
  formFields[payload.field] = payload.value;
  return { status: 'success' };
});
1. Perform RPC Calls from the Backend: Then, on the backend, use
performRpc
on
room.local_participant
to call those frontend methods. Here’s an example:
Copy code
await room.local_participant.perform_rpc(
  destination_identity=client_participant.identity,
  method="updateField",
  payload=json.dumps({
      "field": "currentRole",
      "value": "Software Engineer"
  })
)
If you’d like, I’m here to help with more details or tailor some examples just for your specific situation—feel free to ask! Sources: Tool definition and use | LiveKit Docs | Remote method calls (RPC) | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | Web and mobile frontends | LiveKit Docs | Web and mobile frontends | LiveKit Docs