useDataMessage

useDataMessage

The useDataMessage hook allows you to send and receive messages to and from other peers in the room.

NameTypeDescriptionReturn TypeParams
sendDataFunctionSend a message to other peers in the room.void{ to: string[] | "*"; payload: string; label: string; }

Example Usage

const {
    sendData
  } = useDataMessage({
    onMessage(payload: string, from: string, label?: string) ();
  });
 
const sendMessage = () => {
  sendData({
    to: '*',
    payload: "Hello World!",
    label: "chat"
  });
};

Props

The useDataMessage hook accepts an object with the following fields as props.

onMessage

Optional
DescriptionReturn Type
This function will be called when you receive a data message from any other peer in the room.void
Parameter NameTypeDescriptionRequired
payloadstringThe message string.Yes
fromstringThe peerId of the sender.Yes
labelstringAn optional label field for the message.No

Example Usage

const dataMessage = useDataMessage({ onMessage: (payload, from, label) => {
	console.log("Received a message!");
	console.log("Message: ", payload);
    console.log("Sender: ", from);
    if(label) console.log("Label: ", label);
    // your code here
}});
Audio/Video Infrastructure designed for the developers to empower them ship simple yet powerful Audio/Video Apps.
support
company
Copyright © 2024 Graphene 01, Inc. All Rights Reserved.