how does registering and calling of rpc methods wo...
# ask-ai
b
how does registering and calling of rpc methods work for participants in lviekti agents
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
In LiveKit, participants can register and call RPC (Remote Procedure Call) methods to enable communication between each other. Here's how it works: Registering an RPC Method A participant can register an RPC method using the
registerRpcMethod
function. For example:
Copy code
room.localParticipant?.registerRpcMethod(
  'greet',  // method name
  async (data: RpcInvocationData) => {
    console.log(`Received greeting from ${data.callerIdentity}: ${data.payload}`);
    return `Hello, ${data.callerIdentity}!`;
  },
);
The handler receives the payload and a
responseTimeout
parameter, which indicates the maximum time available to return a response. Calling an RPC Method To call an RPC method on another participant, use the
performRpc
function. For example:
Copy code
try {
  const response = await room.localParticipant!.performRpc({
    destinationIdentity: 'recipient-identity',
    method: 'greet',
    payload: 'Hello from RPC!',
  });
  console.log('RPC response:', response);
} catch (error) {
  console.error('RPC call failed:', error);
}
You can also adjust the
responseTimeout
parameter to specify how long to wait for a response. Error Handling In RPC method handlers, you can throw errors of type
RpcError
with a string message, which will be received by the caller. Other error types will result in a generic "1500" ("Application Error"). If you have further questions or need additional clarification, feel free to ask! Sources: LiveKit JS Client SDK - v2.8.1