Voice chat for browser multiplayer games over WebRTC. Microphone access from a tap with friendly errors (blocked, no microphone, busy, not secure), echo cancellation and noise suppression, mute and push-to-talk, per-player volume and mute, talking indicators, adding voice to existing peer connections (renegotiation), full mesh limits and when an SFU is needed, iframe and HTTPS limits (Bash prev...
Download skill (.zip) Files on GitHubVoice chat integration
Built on webrtc-multiplayer: each pair of players needs a peer connection (a mesh). Audio is sent directly between players. Mesh voice works up to about 6 people; beyond that, each phone sends its voice 5+ times and you need an SFU (a media server such as LiveKit or a hosted service).
assets/voice.js
import { createVoice } from "./voice.js";
const voice = createVoice({ pushToTalk: false });
talkButton.onclick = async () => { // must be inside a click or tap
const r = await voice.start();
if (!r.ok) showMessage(r.message); // denied, no_mic, busy, unsupported: friendly text included
};
for each connected peer: voice.addPeer(id, peer.pc); // adds your microphone, plays theirs
each frame: if (voice.speaking(id)) drawRing(id); // "me" for yourself
voice.setMuted(true); voice.mutePeer(id, true); voice.setVolume(id, 0.5);
push-to-talk: keydown V → voice.talk(true), keyup → voice.talk(false) (pushToTalk: true)
Adding the microphone later triggers renegotiation; createPeer’s perfect negotiation handles it.
Rules
- Off by default. Join voice with a button; show a clear microphone on/off state at all times.
- Muted players: everyone can mute anyone locally. Public games need a report option and should consider friends-only voice.
- Ask for the microphone only when the player chooses voice, never on page load.
- Echo cancellation is on, but suggest headphones.
- Proximity voice:
setVolume(id, 1 - distance / 800)each few frames. - Don’t record or send voice anywhere else without asking.
Where it works
| Place | Notes |
|---|---|
| Direct link over HTTPS | Works (desktop and phones) |
http:// |
Microphone is blocked. Use HTTPS (localhost is allowed) |
| Bash preview panel, itch.io embed | Pages inside an iframe only get the microphone if the parent page allows it. It may fail there: test the game’s direct link, and show the “blocked” message with a hint to open the game in its own tab |
| iOS Safari | Works after a tap; audio may route to the earpiece until the user changes it |
Tested
In Chromium with a fake microphone: microphone starts, voice tracks are added to an already-open peer connection, remote audio plays through an audio element, the talking meter reads levels for yourself and the other player, and mute (own and per player) silences the indicator.
Pitfalls
Starting the microphone without a tap, no way to mute, mesh voice for 10 people, forgetting to stop tracks (voice.stop())
so the browser keeps showing the recording icon, and testing only inside the preview iframe.