This message was deleted.
# helpdesk
s
This message was deleted.
d
you would use it like this:
Copy code
lksdk.ConnectToRoom(url, token, roomCB, lksdk.WithAutoSubscribe(false))
w
Thanks for your answer. I am still hitting a wall trying to receive something via onDataReceived. Both variants, ConnectToRoom and ConnectToRoomWithToken have the same issue. The minimal code seems to be:
Copy code
room, err := lksdk.ConnectToRoom(url, lksdk.ConnectInfo{
   APIKey:              "A....DYA",
   APISecret:           "....So0yCfxc",
   RoomName:            "TestRoom001",
   ParticipantIdentity: "Robot007",
}, &lksdk.RoomCallback{
   ParticipantCallback: lksdk.ParticipantCallback{
      OnTrackSubscribed: onTrackSubscribed,
      OnDataReceived:    onDataReceived,
   },
})
Now the onDatarReceived method should be called when another participant sends something via data channel. For testing i left out the target info (empty array) and thus all participants should receive the data event. Browser based client does, but not the client using the code above. Wierdly enough, if i send something, basically with
Copy code
err = room.LocalParticipant.PublishData(payload,
   livekit.DataPacket_RELIABLE,
   []string{})
The same go code running on the server side is receiving the data. Also the web client. Thus 2 out of 3 participants are receiving but one participant stays in the dark. the two go implementations behave differently. ``````
I used the file transfer example, added my onDataReceived callback and it worked. it is clearly an issue in my code.
e
The simple code to test data channel is:
Copy code
func createAgent(roomName string, callback *RoomCallback, name string) (*Room, error) {
	room, err := ConnectToRoom(host, ConnectInfo{
		APIKey:              apiKey,
		APISecret:           apiSecret,
		RoomName:            roomName,
		ParticipantIdentity: name,
	}, callback)
	if err != nil {
		return nil, err
	}
	return room, nil
}

func TestDatachannel(t *testing.T) {
	pub, err := createAgent(t.Name(), nil, "publisher")
	require.NoError(t, err)

	var dataLock sync.Mutex
	var receivedData string

	subCB := &RoomCallback{
		ParticipantCallback: ParticipantCallback{
			OnDataReceived: func(data []byte, rp *RemoteParticipant) {
				dataLock.Lock()
				receivedData = string(data)
				dataLock.Unlock()
			},
		},
	}
	sub, err := createAgent(t.Name(), subCB, "subscriber")
	require.NoError(t, err)

	pub.LocalParticipant.PublishData([]byte("test"), livekit.DataPacket_RELIABLE, nil)

	require.Eventually(t, func() bool {
		dataLock.Lock()
		defer dataLock.Unlock()
		return receivedData == "test"
	}, 5*time.Second, 100*time.Millisecond)

	pub.Disconnect()
	sub.Disconnect()
}