how can I perform rpc method in agent from react n...
# ask-ai
b
how can I perform rpc method in agent from react native
t
Thanks for reaching out, RikuKomiya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to perform an RPC (Remote Procedure Call) method in an agent from a React Native app using LiveKit, here’s a simple way to do it: Step 1: Register the RPC Method (on the recipient) The participant who’s going to handle the RPC call needs to register a method with
registerRpcMethod
. Here’s an example:
Copy code
room.localParticipant?.registerRpcMethod(
  'getUserLocation',
  async (data) => {
    console.log(`Request from ${data.callerIdentity}: ${data.payload}`);
    // Do something here, like get the device location
    return `Response data`;
  }
);
Step 2: Perform the RPC Call (from the caller) Then, the agent or another participant can call that method using
performRpc
. Check this out:
Copy code
const result = await room.localParticipant.performRpc({
  destinationIdentity: 'user-123',
  method: 'getUserLocation',
  payload: JSON.stringify({ highAccuracy: true }),
});
Just a few things to keep in mind: • The payload needs to be a string (like JSON) and can’t be bigger than 15KiB. • Make sure both participants are connected to the same room. • Don’t forget to handle errors in
performRpc
using try/catch or
.catch()
. If you run into any issues or want to chat about specific use cases, just give me a shout—I’m here to help! Sources: packages/livekit-rtc/README.md | complex-agents/role-playing/role_playing_frontend/components/character-port... | examples/rpc/rpc-demo.ts | Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs
b
destinationIdentityはai agentの場合、どのように指定しますか?
t
Hey there! Could you help me understand how your AI agent is set up in LiveKit? Like, is it joining the room as a participant, and does it have its own identity? Knowing this will make it easier to figure out how to set the
destinationIdentity
. Thanks!
b
room.localParticipant.performRpcが成功したかはどうやって判断できますか?
t
To check if
room.localParticipant.performRpc
worked out, you can handle it just like any other async operation. This method returns a promise that either resolves if everything goes well or rejects if something goes wrong. Here’s a quick example:
Copy code
try {
  const response = await room.localParticipant.performRpc({
    destinationIdentity: 'remote-participant',
    method: 'greet',
    payload: 'Hello!',
  });
  console.log('RPC successful, response:', response); // Got a successful response!
} catch (error) {
  if (error instanceof RpcError) {
    console.error('RPC failed with code:', error.code, 'message:', error.message);
  } else {
    console.error('Unexpected error:', error);
  }
}
Some common reasons it might fail: • `RESPONSE_TIMEOUT`: No response came back in time. • `CONNECTION_TIMEOUT`: There were connectivity problems during the call. • `UNSUPPORTED_METHOD`: The recipient doesn’t recognize that method. • `RECIPIENT_DISCONNECTED`: The other participant disconnected. So, if the promise resolves, that means success, and if it throws an error, something went wrong. Feel free to ask if you want me to walk you through any part of this! Sources: livekit-android-sdk/src/main/java/io/livekit/android/room/participant/Local... | livekit/src/room/participant/local_participant.rs | localparticipant.go | examples/rpc/rpc-demo.ts | lib/src/participant/local.dart
b
room.localParticipant.performRpc return nothing
t
Hey there! If you notice that
room.localParticipant.performRpc
isn’t returning anything, it might mean there’s a hiccup in the RPC flow. No worries though—here are some friendly steps to help you troubleshoot: • Check Method Registration: Make sure the destination participant has registered the method using
registerRpcMethod
. If it’s not registered, the call won’t go through. • Verify Server Version: Double-check that your LiveKit server version is at least 1.8.0, since earlier versions don’t support RPC. • Payload Size: Keep an eye on the payload size—it shouldn’t be bigger than 15KiB. • Timeouts: Sometimes calls hang if the response or connection timeout isn’t set right. Take a look at the
responseTimeout
parameter and confirm the remote participant is online. • Connection Issues: If the remote participant disconnects during the call, it will fail. So, make sure both participants have a stable connection. Also, it’s a good idea to wrap
performRpc
in a
try/catch
block to catch any errors smoothly. Here’s a quick example:
Copy code
try {
  const response = await room.localParticipant.performRpc({
    destinationIdentity: 'remote-identity',
    method: 'yourMethod',
    payload: 'yourPayload'
  });
  console.log('Success:', response);
} catch (error) {
  console.error('RPC failed:', error.message, error.code);
}
If you’re still stuck after this, just give me a shout—I’m here to help you dig in and figure it out together! Sources: Remote method calls (RPC) | LiveKit Docs | packages/livekit-rtc/src/participant.ts | examples/rpc/rpc-demo.ts | livekit-android-sdk/src/main/java/io/livekit/android/room/participant/Local... | lib/src/participant/local.dart