can you point me to the doc for this @room.on("dat...
# ask-ai
a
can you point me to the doc for this @room.on("data_received")
t
Thanks for reaching out, Anne. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The
@room.on("data_received")
event handler is used to listen for data messages received from other participants or server SDKs in a LiveKit room. Here is an example of its usage:
Copy code
@room.on("data_received")
def on_data_received(data_packet: rtc.DataPacket):
    <http://logging.info|logging.info>("received data from %s: %s", data_packet.participant.identity, data_packet.data)
The
data_received
event handler receives a
DataPacket
object with the following properties: •
data
(
bytes
): The payload of the data packet •
kind
(
proto_room.DataPacketKind.ValueType
): Type of the data packet (e.g., RELIABLE, LOSSY) •
participant
(
RemoteParticipant | None
): Participant who sent the data.
None
when sent by a server SDK •
topic
(
str | None
): Topic associated with the data packet Below is a complete integration example showing how to set up the
data_received
event handler within a room connection:
Copy code
import asyncio
import logging
from livekit import rtc

async def main(room: rtc.Room):
    @room.on("data_received")
    def on_data_received(data: rtc.DataPacket):
        <http://logging.info|logging.info>("received data from %s: %s", data.participant.identity, data.data)
    
    # Connect to room and other setup...
    await room.connect(url, token)
Sources: livekit-rtc/livekit/rtc/room.py