early-window-29567
02/27/2026, 9:38 AMtall-airline-16756
02/27/2026, 9:38 AMtall-airline-16756
02/27/2026, 9:39 AMroom.localParticipant.publishData(
new TextEncoder().encode(JSON.stringify({ type: 'position', id: 'char1', x: 100, y: 200 })),
DataPacket_Kind.RELIABLE
);
• Frontend listens for data messages and updates UI:
room.on('dataReceived', (payload) => {
const data = JSON.parse(payload.utf8Value!);
if (data.type === 'position') {
// draw/move dot at (data.x, data.y) for data.id
}
});
• Data messages travel over the same WebRTC connection as audio/video, so delivery is low-latency and can be synchronized with media.
Keeping AI context while streaming positions
• Persist session-level state (for example session.userdata) with key facts the AI needs to retain across interactions (user identity, high-level goals, important flags). This survives agent context truncation.
• Use session.history or a conversation manager to keep full conversation history available for long-term reasoning, while passing only the needed chat_ctx to agents when appropriate.
• If your AI agent needs position data as part of reasoning (e.g., “are two characters close?”), include either:
◦ Recent positions as part of the chat_ctx you send to the agent, or
◦ A pointer/reference in session state so the agent can request current coordinates as needed.
Integration pattern
1. Real-time pipeline:
◦ Agent/backend calculates or receives character positions.
◦ Publish periodic position messages via LiveKit data messages (choose RELIABLE for guaranteed delivery; UNRELIABLE can be used for very high-frequency updates where occasional loss is acceptable).
◦ Frontend receives messages and renders dots/indicators on the screen.
1. AI context:
◦ Store persistent context in session.userdata (identity, persistent goals).
◦ Use session.history for retrieval of full dialogues if agent needs past context.
◦ For handoffs or multiple agents, pass chat_ctx and session pointers so each agent has the minimal context it needs.
Practical tips
• Choose message frequency carefully. For smooth UI movement you might send 10–30 updates/sec; consider interpolation on the client to smooth movement and reduce bandwidth.
• For very high-rate streams, use UNRELIABLE data packets and implement simple smoothing on the client.
• Send an id with each update so the frontend can map positions to the correct character.
• If you need timestamps for synchronization, include a timestamp in each payload.
If you'd like, I can help with:
• A sample implementation for your specific frontend framework (React, plain canvas, etc.).
• Recommended message frequency and packet kind based on your expected update rate.
• How to structure session.userdata and chat_ctx for your AI agent handoffs.
Which frontend tech are you using, and do you expect very high update rates (e.g., >30 updates/sec) or moderate rates?
Sources: Client SDKs | livekit/livekit_composite | DeepWiki | client-sdk-swift | livekit/livekit_composite | DeepWiki | Building multi-agent architectures with LiveKit agents | LiveKit | Using WebRTC + React + WebAudio to create spatial audio | Agents and handoffs | LiveKit Documentationearly-window-29567
02/27/2026, 9:40 AMtall-airline-16756
02/27/2026, 9:40 AMearly-window-29567
02/27/2026, 9:46 AMtall-airline-16756
02/27/2026, 9:46 AM