Valora
API Reference@valora-ai/reactindexFunctions

useDataChannel

function useDataChannel<T, P>(topicOrOnMessage?, onMessage?): UseDataChannelReturnType<T, P>;

Defined in: packages/react/src/hooks/chat/useDataChannel.ts:44

The useDataChannel hook returns the ability to send and receive messages. Pass an optional topic to narrow down which messages are returned in the messages array.

Type Parameters

Type ParameterDefault type
TTrackSource
Punknown

Parameters

ParameterType
topicOrOnMessage?T | ((msg) => void)
onMessage?(msg) => void

Returns

UseDataChannelReturnType<T, P>

Example

// Basic — send and receive messages on one topic.
const { message: latestMessage, send } = useDataChannel("chat", console.log);

// Advanced — wire a room-level data-channel ping.
function DataChannelPanel() {
  const { message: latestMessage, send } = useDataChannel("reactions", (msg) => {
    console.log("reaction received", msg);
  });

  return (
    <VoiceRoom agent={agent}>
      <button onClick={() => send("wave")}>Wave</button>
      {latestMessage && <span>New reaction</span>}
    </VoiceRoom>
  );
}

On this page

Valora is local-first

No API key, no server — everything in this doc runs on-device.

Star on GitHub