clever-thailand-15479
09/02/2025, 7:31 AMpublishData on unity-webgl-sdk. I sent string text every 1000ms and handled DataReceived. but nothing received. I'm really confused.
var data = new
{
type = "heartbeat",
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
message = "Data sent every 100ms"
};
var jsonData = JsonUtility.ToJson(data);
var bytes = Encoding.UTF8.GetBytes(jsonData);
yield return room.LocalParticipant.PublishData(bytes, true, null, "heartbeat");
room.DataReceived += DataReceived;
void DataReceived(byte[] data, RemoteParticipant participant, DataPacketKind? kind)
{
UnityLogger.Logger.Info("DataReceived {0}, {1}", data.Length, participant.Identity);
string data_str = Encoding.ASCII.GetString(data);
UnityLogger.Logger.Info(data_str);
}tall-airline-16756
09/02/2025, 7:31 AMtall-airline-16756
09/02/2025, 7:32 AMDataReceived event might not be firing in your Unity WebGL SDK setup:
• Event Subscription Timing: Make sure you’re subscribing to the DataReceived event before or right after the room connects. If PublishData runs before the connection is ready or before the event handler is set up, the data might not come through.
• Reliable vs. Lossy Packets: Try using DataPacketKind.RELIABLE to guarantee delivery, since LOSSY packets can get dropped if the network is busy.
• Topic and Destination Filters: Double-check that the participant receiving the data is subscribed to the right topic and that no filters in PublishData are blocking it.
• Serialization Consistency: It’s important that the data is serialized and deserialized the same way on both ends—otherwise, the packet might not be processed correctly.
• Debugging: You can turn on debug logs by adding LK DEBUG to your Unity player’s scripting define symbols. This helps you see if data is coming in but maybe not handled properly.
Here’s a quick example you can try out:
Room.DataReceived += (data, participant, kind) =>
{
Debug.Log("Received data : " + Encoding.ASCII.GetString(data));
};
yield return Room.LocalParticipant.PublishData(Encoding.ASCII.GetBytes("This is a test"), DataPacketKind.RELIABLE);
Just make sure the room is fully connected, the event handler is attached, and no participant filters are stopping the data. If you hit any bumps or want to chat more about this, I’m here to help!
Sources: LivekitUnitySampleApp/Assets/LivekitSamples.cs | unity-project/Assets/GameScene.unity | Runtime/Scripts/Room/Participant/Participant.csclever-thailand-15479
09/02/2025, 7:38 AMtall-airline-16756
09/02/2025, 7:39 AMPublishData setup:
• Topic: Double-check that the topic you’ve set in PublishData matches what the receiving participants expect. If you haven’t specified a topic, your data should be received by everyone without any filtering.
• Destination Filters: Take a look at the destinationIdentities parameter in PublishData. It should either be null or include the identity of whoever you want to receive the data. If it’s set, only those specific participants will get the data.
Here’s an example from your code:
yield return room.LocalParticipant.PublishData(bytes, true, null, "heartbeat");
What this means: The third parameter (null) means there’s no destination filter, and the fourth parameter ("heartbeat") is the topic. Just make sure the participant on the other end is subscribed to that same topic (or that there’s no topic filtering in place).
If you’re still running into trouble, just let me know! I’m happy to help you troubleshoot further.clever-thailand-15479
09/02/2025, 7:40 AMtall-airline-16756
09/02/2025, 7:40 AMPublishData, you just need to use the topic parameter while calling the method. This helps you separate different types of data packets so the receiver can handle them selectively.
Here’s a quick example:
byte[] myData = Encoding.UTF8.GetBytes("Hello, World!");
localParticipant.PublishData(myData, null, true, "chat");
In this case, the data is sent with the topic "chat". On the receiving end, you can check the topic field in the DataReceived event to filter and handle the data just the way you want.
If you have any questions or want me to walk you through it, just let me know—I’m here to help!
Sources: Runtime/Scripts/Proto/DataStream.cs | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | Data packets | LiveKit Docsclever-thailand-15479
09/02/2025, 7:41 AMLK DEBUGtall-airline-16756
09/02/2025, 7:42 AMLK_DEBUG in your Unity project to see those helpful internal LiveKit logs, here’s a quick guide:
1. Open up your Unity project.
2. Head over to Project Settings > Player.
3. Look under Other Settings for Scripting Define Symbols.
4. Add LK_DEBUG to the symbols list for the platform you’re working on (like WebGL).
5. Rebuild your project.
After that, you’ll start seeing debug messages in the console, which can really help when you’re troubleshooting. If you get stuck or want a hand with anything else, just let me know—I’m here to help!
Sources: Documentation~/api/index.mdclever-thailand-15479
09/02/2025, 7:54 AMpublishdata is defined in localParticipant.cs as below
public JSPromise PublishData(byte[] data, bool reliable, string[] destinationIdentities, string topic)
public JSPromise PublishData(byte[] data, int offset, int size, bool reliable, string[] destinationIdentities, string topic)
It is different from the example.tall-airline-16756
09/02/2025, 7:54 AMPublishData method in localParticipant.cs actually has two handy overloads, giving you some nice flexibility in how you send your data:
• `PublishData(byte[] data, bool reliable, string[] destinationIdentities, string topic)`: This one sends the whole byte array. You can decide if the data should be sent reliably, pick who gets it (or use null to send to everyone), and set the topic.
• `PublishData(byte[] data, int offset, int size, bool reliable, string[] destinationIdentities, string topic)`: This lets you send just a part of the byte array by specifying where to start (offset) and how many bytes to send (size). The rest works just like the first overload.
For example, if you want to send the entire byte array reliably to all participants with a topic, you’d do something like this:
byte[] myData = Encoding.UTF8.GetBytes("Hello, World!");
localParticipant.PublishData(myData, true, null, "chat");
And if you only want to send part of the byte array, here’s how:
byte[] myData = Encoding.UTF8.GetBytes("Hello, World!");
localParticipant.PublishData(myData, 0, 5, true, null, "chat");
This will send just the first 5 bytes of the array. If you have any questions or want me to explain anything else, just let me know—I’m happy to help!clever-thailand-15479
09/02/2025, 8:14 AMtall-airline-16756
09/02/2025, 8:15 AM