I want to stream an audio source from a website /host to another page /listen on the same website.
The principle seems to be a bit like an online radio, the mini player on YouTube or streaming services like Tidal or Spotify, where you can listen to the music while switching to another page on the website but the listener is never visiting /host. The listener hears what the host is saying on /host. A simplified graphics could look like this:
// ┌─────────────────────┐
// │ │
// │ Host PC │
// │ │
// │ with microphone │
// │ │
// └───────────────┬─────┘
// │
// ┌───────────────▼─────┐
// │ │
// │ Webserver │
// │ │
// └───────────────┬─────┘
// │
// ┌───────────────▼─────┐
// │ │
// │ Admin panel page │
// │ │
// │ with input source │ This page the host
// │ │ is visiting.
// │ https://x.y/host │
// │ │
// └───────────────┬─────┘
// │
// │
// ┌───────────────▼─────┐
// │ │
// │ User page │
// │ │
// │ listening to the │ This page the listener
// │ stream │ is visiting.
// │ │
// │ https://x.y/listen │
// │ │
// └─────────────────────┘
Via the Web Audio API I can setup an AudioContext and can add my microphone as a source via the WebRTC API, but I don't know how to connect it to another page:
window.AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
const microphone = audioCtx.createMediaStreamSource(stream);
microphone.connect(audioCtx.destination);
});
What would be the "best practice" way of achieving this expected behaviour?