Not able to connect to realtime server side websocket using call_id

Hello Community,

I am building a realtime chatbot using openai Realtime API.

This is what I have already implemented:

  1. From server creating a session using /v1/realtime/sessions, getting the sessionID and sending to the client

  2. Getting the session_id from the server and through client connecting to WebRTC using v1/realtime/calls?model=gpt-realtime

Now what I want to accomplish is from server somehow getting the transcript of the session, one easy way is to send the transcript from client to server and that is what I am doing currently. But I find it to highly inefficient.

But I noticed /docs/guides/realtime-server-controls in API Docs, which enables server to connect to the realtime call using call ID.

Reading this I have successfully captured the callID, but when I am trying to connect to the websocket at the endpoint “wss://api.openai.com/v1/realtime?call_id=<call_id_here>” I am getting 404.

Would greatly appreciate community insight as to what am I doing wrong here. Happy to provide more context and excerpts of code if required.

1 Like

I got the solution. The issue is in the documentation.

This is what they have suggested:

import WebSocket from "ws";

// You'll need to get the call ID from the browser to your
// server somehow:
const callId = "rtc_u1_9c6574da8b8a41a18da9308f4ad974ce";

// Connect to a WebSocket for the in-progress call
const url = "wss://api.openai.com/v1/realtime?call_id=" + callId;
const ws = new WebSocket(url, {
    headers: {
        Authorization: "Bearer " + process.env.OPENAI_API_KEY,
    },
});

ws.on("open", function open() {
    console.log("Connected to server.");

    // Send client events over the WebSocket once connected
    ws.send(
        JSON.stringify({
            type: "session.update",
            session: {
                type: "realtime",
                instructions: "Be extra nice today!",
            },
        })
    );
});

// Listen for and parse server events
ws.on("message", function incoming(message) {
    console.log(JSON.parse(message.toString()));
});

Here, just replace OPENAI_API_KEY with the ephemeral key generated using either realtime/client_secret API or realtime/sessions API.

It will work.

4 Likes

Hi we are also receiving a 404 when connecting to the websocket but we are using SIP instead of webRTC and therefore we don’t have any ephemeral key, just the normal api key and the call_id.

Let’s see if someone can bring some light on this! All we see is a 404 error when connecting to the websocket by following this https://platform.openai.com/docs/guides/realtime-sip

EDIT: Solved! The issue was on our accept payload, we were passing too many arguments and even though the accept response was a 200 the call id was not correct for the SIP call

2 Likes

Since the websocket connection (for server-side controls) is authorized with the ephemeral key, it would be great to support the standard WebSocket constructor, available in Node v22 (and browsers), without the need for libraries such as ws. The constructor takes an optional list of sub-protocols (strings). Example:

const url = "wss://api.openai.com/v1/realtime?call_id=" + callId;
const ws = new WebSocket(url, [
    `openai-ephemeral-key.${ephemeralKey}`
]);

ws.addEventListener("open", function open() { /* ... */ });

Thanks man, This worked for me.

I am however getting like a minute of talk time then there is no the model just stops. Anyone experiencing the same? Have you found a solution?