rough-holiday-12077
02/15/2025, 7:29 PMgray-monkey-83525
02/15/2025, 7:29 PMpublishData
method to send custom data packets to the room. This allows you to send user-specific information or prompts that the agent can use to adjust its behavior. According to the LiveKit documentation, you can publish data messages to other participants in the same room using LocalParticipant.publishData
.
Here's a basic example of how you might implement this in Flutter:
import 'package:livekit_client/livekit_client.dart';
final room = await LiveKitClient.connect(url, token);
final data = {
'type': 'user_prompt',
'prompt': 'User is behaving in a specific way',
};
await room.localParticipant.publishData(
Uint8List.fromList(utf8.encode(json.encode(data))),
reliable: true,
topic: 'user_behavior',
);
This code sends a JSON-encoded message to the room, which the agent can then interpret and respond to accordingly. Would you like more detailed guidance on this process?
Sources:
- Data messages | LiveKit Docs π 1 button π 0 button π‘ Hint: Mention @millions-hair-24956 in the thread for followups.rough-holiday-12077
02/15/2025, 7:38 PMstatic setNewContext() {
print("sent prompt");
final data = {
'type': 'user_prompt',
'prompt': 'Say Hi, and start the user onboarding',
};
activeRoom!.localParticipant?.publishData(
Uint8List.fromList(utf8.encode(json.encode(data))),
reliable: true,
topic: 'user_behavior',
);
}
rough-holiday-12077
02/15/2025, 7:39 PMgray-monkey-83525
02/15/2025, 7:39 PM@ctx.room.on("data_received")
async def on_data_received(data_packet):
message = json.loads(data_packet.data.decode('utf-8'))
if message['type'] == 'user_prompt':
prompt = message['prompt']
# Process the prompt, e.g., make the agent say "Hi"
await agent.say(prompt)
3. Debugging: Ensure that the data is being sent correctly from the Flutter app and that the agent is connected to the room and ready to receive messages.
Would you like more detailed guidance on setting up the agent to handle these messages?
Sources:
- https://docs.livekit.io/client-sdk-js/#md%3Aaudio-playback
- https://docs.livekit.io/client-sdk-js/index.html#md%3Aaudio-playback π 0 button π 0 button π‘ Hint: Mention @runllm in the thread for followups.rough-holiday-12077
02/15/2025, 7:40 PMgray-monkey-83525
02/15/2025, 7:40 PMrough-holiday-12077
02/15/2025, 8:02 PMgray-monkey-83525
02/15/2025, 8:02 PMimport asyncio
import json
from livekit import rtc
async def handle_data_received(data_packet, ctx, agent):
try:
# Decode the data from the DataPacket
message = json.loads(data_packet.data.decode('utf-8'))
if message['type'] == 'user_prompt':
prompt = message['prompt']
# Make the agent say the prompt
await agent.say(prompt)
except Exception as e:
print(f"Error handling data received: {str(e)}")
async def entrypoint(ctx):
await ctx.connect(auto_subscribe=rtc.AutoSubscribe.AUDIO_ONLY)
participant = await ctx.wait_for_participant()
# Initialize your agent here
agent = YourAgentClass()
# Register data received handler
ctx.room.on("data_received", lambda dp: asyncio.create_task(handle_data_received(dp, ctx, agent)))
# Keep the connection alive
while True:
await asyncio.sleep(1)
# Run the agent
if __name__ == "__main__":
from livekit.agents import cli, WorkerOptions
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Key Points:
- Data Handling: The handle_data_received
function decodes the incoming data packet and checks if it contains a user_prompt
. If it does, it makes the agent say the prompt.
- Agent Initialization: Ensure your agent is properly initialized and capable of responding to the say
method.
- Event Registration: The ctx.room.on("data_received", ...)
line registers the handler to listen for data messages.
Make sure your agent is connected to the same room and is ready to receive messages. If you need more detailed guidance, feel free to ask!
Sources:
- https://livekit-users.slack.com/archives/C07FY8WHGPM/p1731565699.055979
- https://livekit-users.slack.com/archives/C07FVFM5NA1/p1733833739.342519 π 0 button π 0 button π‘ Hint: Mention @runllm in the thread for followups.