Data Messages
You can send arbitrary data messages to participants in a room using the
LocalParticipant
's publishData
method. Data can be sent to all participants in the room or to specific participants and can be sent reliably or unreliably.import { RoomEvent, DataPacket_Kind, RemoteParticipant } from "@atmokyaudio/engage-client"
const room = space.rooms[0];
const localParticipant = room.localParticipant;
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const textData = JSON.stringify({welcome: "Hello World!"});
const data = encoder.encode(textData);
// Send data to all participants in the room
localParticipant.publishData(data, DataPacket_Kind.LOSSY);
// Send data to a specific participant in the room
localParticipant.publishData(data, DataPacket_Kind.RELIABLE, ['participantSid']);
// Receive data from other participants in the room
room.on(RoomEvent.DataReceived, (payload: Uint8Array, participant?: RemoteParticipant, kind?: DataPacket_Kind, topic?: string) => {
const receivedData = decoder.decode(payload);
console.log("Received ", receivedData, "from ", participant?.identity, "with kind ", kind, "on topic ", topic);
})