This message was deleted.
# helpdesk
s
This message was deleted.
p
yes, there is a
useDataChannel
hook that returns a `send`method
j
thanks, is there any method where I can receive sent message
I can see there is
onMessage
parameter, but don't know how to use it
Is there any example how to use it, I am facing errors
Getting this error
r
Hello, I've been trying to create a project similar to twitter spaces, but I'm stuck on creating the reactions. I've found the
useDataChannel
react hook that should manage all the logic from the client SDK, I'm encoding and decoding messages, but it seems that once I send one, the
isSending
is always true and other participants never get the message. No error is ever logged or anything, is there a standard way of sending ephemeral messages to all participants? Is sending encoded emojis ok? Any help is appreciated. Here's my code
Copy code
const { message, send, isSending } = useDataChannel("reactions", (message) => {
    console.warn(
      `%cmessage ||${JSON.stringify(message, null, 2)}|| decoded ${decoder.decode(message?.payload)}`,
      "🐦;background: lightblue; color: #444; padding: 3px; border-radius: 5px;"
    );
  }); 

[...]

const data = encoder.encode("👍");
send(data);
p
@jolly-rocket-99624 you have to call the hook in a child component of <LiveKitRoom/> currently you are missing the room context provider for the hook to function properly
j
Thank you so much for quick guide, I will follow the same
p
@red-father-14173 that’s unexpected. which version are you on, and do you observe the same in a “fresh” project, e.g. just adding the data channel functionality to one of the examples in the repo?
j
"livekit-client": "^1.9.6",
r
My versions are the following, haven't tested this on a new project.
Copy code
"livekit-client": "^1.11.3",
"@livekit/components-react": "^1.0.6",
"@livekit/components-styles": "^1.0.2"
Just as an addendum, I'm using the chat example from the livekit stream repo without any issues
j
So,I need to upgrade packages then I will try
p
@red-father-14173 does it work if you wrap the `send`method in useEffect?
Copy code
React.useEffect(() => {
    send(new TextEncoder().encode('test'), { kind: DataPacket_Kind.RELIABLE });
  }, []);
r
Hmm, let me try. But I'm sure the
send
function is being called as the
isSending
is returning true after
I'm wondering, by sending a message, the
onMessage
from
useDataChannel
hook runs for every participant, including the one that sent the message? Or just for every other participant?
Ok, so running with 2 participants for 2 minutes on different browsers but same machine, here's what I got. • first participant to join never received a message; • second participant to join got 4 messages in total (it ran until I finished writing this and it didn't go up); •
isSending
remained true at all times after joining the room;
p
if you send a message from participantA, participantA will never receive the message, the message gets only sent to every other participant, but not to itself
r
I see, but is there a reason why even sending reliable messages other participants didn't get most of the messages? Is there a way for me to debug this? I'm quite confused since when using the chat component from the livestream repo messages also get lost but just from one participant, see screenshots below. I'd like to have the same functionality in my reactions as in chat, where participantA sees the reaction and all other participants see it, and when participantB sends a reaction, the same thing happens.
p
hm, that’s definitely odd. we currently don’t have debug messages around data messages in the react components. you could enable the debug logs for the client-sdk by importing
setLogLevel('debug')
from livekit-client directly. you should see
DataReceived
events in the console then.
are there any other errors in the console?
r
no errors related to livekit are logged
I was looking at the
ParticipantPermissions
and couldn't find the defaults for
canPublishData
when
canPublish
is set to false. The data is sent correctly once I set
canPublish
to true on all participants, but if set to false, they can no longer publish data.
Although this doesn't explain why the
isSending
in the
useDataChannel
stays true even after sending the data.
It also looks like
canPublishData
is false by default (or I may just not be familiar with the whole codebase) https://github.com/livekit/server-sdk-js/blob/main/src/proto/livekit_models.ts#L1275 Unfortunately I don't control the server I'm using to test if data messages work with
canPublish
set to
false
and
canPublishData
set to
true
, will check with the responsible team.