refined-hydrogen-81988
10/31/2025, 6:42 PMtall-airline-16756
10/31/2025, 6:43 PMtall-airline-16756
10/31/2025, 6:43 PMroom.sendData() or participant.publishData().
To make things clearer, here’s a simple example of a custom React frontend you might find helpful:
import { Room } from 'livekit-client';
import React, { useMemo, useEffect, createContext, useContext } from 'react';
const LiveKitContext = createContext<Room | null>(null);
export const useLiveKit = () => {
const room = useContext(LiveKitContext);
if (!room) throw new Error("useLiveKit must be used inside LiveKitProvider");
return room;
};
export function LiveKitProvider({ children }: { children: React.ReactNode }) {
const room = useMemo(() => new Room(), []);
useEffect(() => {
if (room.state === 'disconnected') {
room.connect('wss://your-livekit-server', 'your-participant-token')
.catch(err => console.error("Failed to connect", err));
}
room.on('dataReceived', (payload, participant) => {
console.log('Data from', participant.identity, ':', payload.toString());
});
return () => {
room.disconnect();
};
}, [room]);
return (
<LiveKitContext.Provider value={room}>
{children}
</LiveKitContext.Provider>
);
}
This approach uses React context to share the Room instance and gracefully handle events like dataReceived. And remember, you can always send data back using room.localParticipant.publishData().
If you’d like to dive deeper or see more examples, I warmly recommend checking out the Web and mobile frontends documentation or exploring the Note-taking Assistant frontend. They’re great resources to guide you along the way!
Sources: Agent Testing & CI/CD | livekit/livekit_composite | DeepWiki | lib/src/context/room_context.dart | Rooms, participants, and tracks | LiveKit docs | Connecting to LiveKit | LiveKit docs | Web and mobile frontends | LiveKit Docs