Developer Documentation

OdinMessageReceivedEvent

type OdinMessageReceivedEvent = (event: IOdinEvent<IOdinMessageReceivedEventPayload>)

Discussion

The OdinMessageReceivedEvent event is emitted on OdinRoom and OdinPeer instances when receiving a message from another peer in the room. Note that this event only provides the ID of the peer as senderId. This is because the invoking peer might not be in proximity due to server-side culling.

Events are inheriting from IOdinEvent and provide an object described by IOdinMessageReceivedEventPayload in the payload property of the event.

Event Scopes

  • IOdinRoomEvents
  • IOdinPeerEvents

Example

OdinMessageReceivedEvent Example
import { OdinClient } from '@4players/odin';

const startOdin = async function (token: string) {
  // Authenticate using a token obtained externally and spawn a room instance
  const odinRoom = await OdinClient.initRoom(token);

  // Get notified when a message with arbitrary data was received (data is encoded as Uint8Array)
  odinRoom.addEventListener('MessageReceived', (event) => {
    console.log(`Received data from peer ${event.payload.senderId}:`, event.payload.message);
  });

  // Join the room
  odinRoom.join();
};

startOdin('__YOUR TOKEN__').then(() => {
  console.log('Started ODIN');
});

Parameters

NameTypeDescription
eventIOdinEvent<IOdinMessageReceivedEventPayload>