when in worker we use self._room.local_participant...
# ask-ai
e
when in worker we use self._room.local_participant.send_text(topic="conversation_stage", text="done") on the client side what event it trigerred and how can I listen to that event? will this work on the client side for send_text room.on(RoomEvent.DataReceived, onData);
t
Thanks for reaching out, Shubhankar. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! The
room.on(RoomEvent.DataReceived, onData)
event handler is perfect for receiving messages sent using
send_text
in LiveKit. When data comes in from another participant, the
DataReceived
event is triggered. Messages sent with
send_text
are sent as data packets with the MIME type
text/plain
, and these packets are delivered to everyone in the room who’s listening for the
DataReceived
event. If you’d like, I’m here to help with any further clarification or examples you might need! Sources: test/core/data_stream_test.dart | livekit-rtc/livekit/rtc/room.py | src/room/events.ts | livekit/tests/data_stream_test.rs | livekit-android-test/src/test/java/io/livekit/android/room/RoomDataMockE2ET...
e
what if worker do this instead self._room.local_participant.publish_data(topic="conversation_stage", text="done")
t
When
publish_data
is used in a LiveKit worker, the client-side event triggered is also
DataReceived
. This event is emitted on the
Room
object whenever data is received from another participant or the server. You can listen for this event just like you would with
send_text
. For example, in the JavaScript SDK:
Copy code
room.on('dataReceived', (payload, participant, topic) => {
  console.log('Received data:', payload);
  // Handle the received data
});
The
topic
field is really handy for filtering messages, and you can also target specific participants if that fits your needs. Please feel free to reach out if you'd like me to walk you through this further or if you have any other questions—I’m here to help! Sources: Handling events | LiveKit Docs | src/room/participant/LocalParticipant.ts | Data packets | LiveKit Docs
e
this is my client code useEffect(() => { if (!room) return; const onData = ( payload: Uint8Array, participant?: Participant, _kind?: DataPacket_Kind, topic?: string ) => { console.log("onData", payload, participant, _kind, topic) if (topicSet && (!topic || !topicSet.has(topic))) return; let text = ''; try { text = new TextDecoder().decode(payload); } catch {} setTextStreams((prev) => [ ...prev, { text, topic, from: participant?.identity, at: Date.now() }, ]); }; room.on(RoomEvent.DataReceived, onData); return () => { room.off(RoomEvent.DataReceived, onData); }; }, [room, topicSet]); I never see this log console.log("onData", payload, participant, _kind, topic)
t
Thank you for reaching out! Let’s explore some possible reasons why
RoomEvent.DataReceived
might not be triggering in your client code, along with some friendly debugging tips: 1. No Data Being Sent: First, please check if the worker or another participant is actively sending data using
publish_data
or a similar method. 2. Event Listener Not Subscribed: It’s important to make sure the
DataReceived
listener is properly attached to the
Room
object, either before or after connecting. 3. Room State Issues: Confirm that the room is fully connected before expecting data. You can subscribe to
RoomEvent.Connected
to keep track of the connection state. 4. Data Channel Not Established: Check if the WebRTC data channel has been set up correctly. Look out for any logs indicating the creation of the data channel. 5. Payload Format or Kind Mismatch: Make sure both sender and receiver are using compatible data kinds, like
DataPacket_Kind.RELIABLE
. 6. E2EE or Encryption Issues: If you’re using end-to-end encryption, please ensure the keys are configured properly. For testing, you might try temporarily disabling E2EE to see if that helps. For a deeper dive, you can log the connection state, data channel status, and any incoming data packets. I’m here to help if you want to walk through specific debugging steps or need more details—just let me know! Sources: src/room/Room.ts | Runtime/Scripts/Events.cs | Runtime/Scripts/Room.cs | Connecting to LiveKit | LiveKit Docs | Handling events | LiveKit Docs