hi. I'm using `publishData` on unity-webgl-sdk. I ...
# ask-ai
c
hi. I'm using
publishData
on unity-webgl-sdk. I sent string text every 1000ms and handled
DataReceived
. but nothing received. I'm really confused.
Copy code
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");
Copy code
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);
}
t
Thanks for reaching out, Altria. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Let’s look at some common reasons why the
DataReceived
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:
Copy code
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.cs
c
I'm sure DataReceived is subscribed before room.connect. how can I check topic and destination filters
t
Let’s make sure that the topic and destination filters aren’t causing any hiccups in your
PublishData
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:
Copy 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.
c
where to set topic filtering?may i ask.
t
Hey there! When you want to set topic filtering in
PublishData
, 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:
Copy code
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 Docs
c
tell me how to set
LK DEBUG
t
If you want to enable
LK_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.md
c
I see that
publishdata
is defined in localParticipant.cs as below
Copy code
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.
t
Hey there! The
PublishData
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:
Copy code
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:
Copy code
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!
c
I still cannot receive the DataReceive event
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!