useDataMessage
The useDataMessage hook allows you to send and receive messages to and from other peers in the room.
const {
sendData
} = useDataMessage({
onMessage(payload, from, label) {},
});
Props
The useDataMessage hook accepts an object with the following fields as props.
1. onMessageOptional
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
}});
Returns
The useDataMessage hook returns an object with the following fields.
1. sendDataFunction
Description | Return Type |
---|---|
Send a message to other peers in the room. | Promise<Room> |
Parameter Name | Type | Description | Required |
---|---|---|---|
data | { to: string[] | "*"; payload: string; } | Object containing the message to be sent (payload) and an array of peerIds to send the message to (you can use a "*" to denote all peers in the room). | Yes |