We have a LiveKit Agent that uses `publishData` fr...
# ask-ai
c
We have a LiveKit Agent that uses
publishData
from the Local Participant to send an initial message to the frontend right when the agent starts. Locally everything works, and it also works on our DEV domain, but on our PROD domain (different domain) this specific
publishData
is completely ignored — the client never receives it and there are no errors. Other
publishData
messages in our project work fine; only this very first, “startup” message fails on PROD. Same code and SDK versions across environments. Tokens and grants look correct (
canPublishData
etc.), topic names match, payload is small, and media (audio/video) works on PROD. We also tried toggling reliability and adding a short delay before sending — sometimes helps, but not consistently. It feels like a timing/race issue where the agent publishes before clients are ready on PROD, but not on DEV. What could be the cause of
publishData
being ignored only for this initial message on PROD? Does LiveKit deliver
publishData
only to participants already connected at that moment (i.e., no buffering for late joiners)? Any recommended pattern to guarantee the initial payload reaches clients on startup (e.g., wait for participants to join, republish on participant join, or target specific identities)? Any diagnostics we can enable to confirm the message leaves the agent and reaches the server?
t
Thanks for reaching out, Babken. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! Just to share a bit about LiveKit's `publishData`: it sends messages only to participants who are connected at the exact time the message goes out. So, if someone joins a little later, they won’t get those earlier messages since there’s no buffering for late joiners. To help make sure your initial payload reaches everyone when they start up, here are a few friendly suggestions you might find useful: • Wait for Participants: Hold off on sending that first message until you’re sure all the expected participants have joined in. • Republish on Join: Keep an eye out for new participants joining and send the initial message to them again. • Use RPC for State Sync: You could have new participants request the current state from existing ones using LiveKit’s RPC system—this helps keep everyone in sync. Also, if you want to dig a bit deeper, you can turn on diagnostics by logging the
publishData
calls and checking your server logs to see that messages are being sent and received properly. If you’d like, I’m here to help with any details on setting these up—just let me know! Sources: Data packets | LiveKit Docs | Sending text | LiveKit Docs | web/providers/GameProvider.tsx
c
how to wait for participants before sending?
t
To make sure you wait for participants before sending
publishData
, here’s a helpful approach you can try: 1. Check Existing Participants: Take a look at the
room.remoteParticipants
array to see if anyone is already connected. 2. Listen for New Participants: You can subscribe to the
participant_connected
event to know when new participants join the room. 3. Publish Once Ready: When you’re sure participants are connected, go ahead and send your
publishData
message. Here’s a simple example in JavaScript that might help:
Copy code
// Check existing participants
if (room.remoteParticipants.size > 0) {
    sendInitialMessage();
} else {
    // Listen for new participants
    room.on('participant_connected', (participant) => {
        sendInitialMessage();
    });
}
If you’d like, I’m here to help you tailor this to your specific SDK or environment—just let me know! Sources: livekit-android-sdk/src/main/java/io/livekit/android/room/RTCEngine.kt | cmd/lk/room.go | Tests/LiveKitCoreTests/PublishDataTests.swift | examples/rpc.py | examples/demo/demo.ts