Perform Remote Procedure Calls (RPC)
mainLiveKit RPC allows participants to call predefined methods on other participants. This is useful for triggering actions in a client application from an Agent or another user.
- Register a method: Use
room.localParticipant.registerRpcMethod(name, handler)to define a method that can be called remotely. The handler receivesRpcInvocationData(includingpayloadandcallerIdentity) and should return a response. - Perform a request: Use
room.localParticipant.performRpc({ destinationIdentity, method, payload })to invoke a method on a specific participant.
Errors: If a handler throws an RpcError, the message is passed to the caller. Other errors arrive as a generic 1500 ("Application Error").
// 1. Registering a method
room.localParticipant?.registerRpcMethod(
'greet',
async (data: RpcInvocationData) => {
return `Hello, ${data.callerIdentity}!`;
}
);
// 2. Performing the call
try {
const response = await room.localParticipant!.performRpc({
destinationIdentity: 'recipient-identity',
method: 'greet',
payload: 'Hello!',
});
console.log(response);
} catch (error) {
console.error('RPC call failed:', error);
}