useDataMessage
The useDataMessage hook allows you to send and receive messages to and from other peers in the room.
Name | Type | Description | Return Type | Params |
---|---|---|---|---|
sendData | Function | Send 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
Description | Return Type |
---|---|
This function will be called when you receive a data message from any other peer in the room. | void |
Parameter Name | Type | Description | Required |
---|---|---|---|
payload | string | The message string. | Yes |
from | string | The peerId of the sender. | Yes |
label | string | An 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
}});