How do I send a message from participant (Javascri...
# ask-ai
p
How do I send a message from participant (Javascript client) to the agent (Python) without adding it to history? And how does the agent receive and send a message back to the participant?
t
Thanks for reaching out, Bilal. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To send a message from a participant (JavaScript client) to an agent (Python) without adding it to the chat history, you can use the
publish_data
method with a custom topic. This ensures the message is not tracked in the chat history. Here's an example:
Copy code
// JavaScript example
await room.localParticipant.publishData(
    payload: JSON.stringify({ message: "Your message here" }),
    topic: "custom-topic-not-for-chat"
);
On the agent's side (Python), you can listen for these messages by setting up a data receiver for the specific topic:
Copy code
# Python example
@room.on("data_received")
def on_data_received(dp: DataPacket):
    if dp.topic == "custom-topic-not-for-chat":
        try:
            parsed = json.loads(dp.data)
            message = parsed["message"]
            # Handle the message
        except Exception as e:
            logging.warning("Failed to parse message: %s", e, exc_info=e)
To send a message back to the participant, the agent can use the
send_message
method of the `ChatManager`:
Copy code
# Python example
await chat_manager.send_message("Hello from the agent!")
Let me know if you need further assistance! Sources: Job lifecycle | LiveKit Docs | livekit.rtc.chat API documentation | livekit.rtc.chat API documentation