React
This guide walks through integrating the ODIN Voice Web SDK into a React application. It covers every step from plugin initialization to cleanup, using idiomatic React patterns (hooks, refs, state). The code below is based on the tested example in the ODIN Web SDK repository.
This example uses @4players/odin-tokens to generate tokens client-side for simplicity. In production, tokens should be generated on your backend server to protect your access key. See Getting Started for details.
Dependencies
Full Example
Step-by-Step Breakdown
Plugin
The audio plugin is registered automatically by the SDK when needed -- no manual initialization is required. See Customize the Plugin if you need to customize the plugin.
Modern browsers require a user gesture (click/tap) before an AudioContext can be started. Ensure joinRoom is called from a button click handler.
Event Handlers
All event handlers must be registered before calling room.join(). The example registers four handlers:
| Handler | Purpose |
|---|---|
room.onJoined | Update connection state when successfully joined |
room.onLeft | Reset state when disconnected |
room.onPeerJoined | Track peers; attach per-peer onAudioActivity for speaking detection |
room.onPeerLeft | Remove peer from state |
The per-peer PeerEventsonAudioActivity handler receives { media } where media.isActive indicates whether the peer is currently speaking.
Joining and Audio
Two critical calls must happen in order:
setOutputDevice({})— Must be called in order to hear other peersroom.addAudioInput(audioInput)— Attaches the microphone and starts audio transmission
Muting
The example uses the full mute approach for maximum resource savings. See Muting & Volume Control for all available approaches.
Cleanup
The useEffect cleanup function ensures audio is stopped and the room is left when the component unmounts, preventing resource leaks.
Related
- Getting Started — Step-by-step integration guide
- Muting & Volume Control — All muting strategies explained
- Push to Talk — PTT using
volumeGate - Working with Events — Full event reference
- Working with Devices — Device selection and switching