Skip to content

Instantly share code, notes, and snippets.

@bran921007
Created December 18, 2024 01:48
Show Gist options
  • Save bran921007/3593c5429fc859a990e70b663de1553e to your computer and use it in GitHub Desktop.
Save bran921007/3593c5429fc859a990e70b663de1553e to your computer and use it in GitHub Desktop.
The Realtime API with WebRTC - OpenAI api
async function createRealtimeSession(inStream, outEl, token) {
const pc = new RTCPeerConnection();
pc.ontrack = e => outEl.srcObject = e.streams[0];
pc.addTrack(inStream.getTracks()[0]);
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
const headers = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/sdp' };
const opts = { method: 'POST', body: offer.sdp, headers };
const resp = await fetch('https://api.openai.com/v1/realtime', opts);
await pc.setRemoteDescription({ type: 'answer', sdp: await resp.text() });
return pc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment