Realtime api websocket disconnects randomly in nodejs

Using nodejs connecting from server side.

I’m running into this issue that some time around 5-10 minutes in - response.text.delta server event starts to respond slowly and my websocket connection disconnects.

I then get error RealtimeAPI is not connected from here: openai-realtime-api-beta/lib/api.js at main · openai/openai-realtime-api-beta · GitHub with eventName="input_audio_buffer.append".

Modifying the close listener here: openai-realtime-api-beta/lib/api.js at main · openai/openai-realtime-api-beta · GitHub to ws.on('close', (code, reason) I get code=1011 with reason="keepalive ping timeout"

Looking at the rate_limits.updated event. I’m hitting neither the token limit cap, or the 15-minute session timer cap.

Is there another mechanism from OpenAI side that throttles requests and would cause this disconnect?

2 Likes

I figured it out—you need to monkey patch the api.js in the library with a keep-alive ping-pong mechanism. The base library currently doesn’t support long-running monologues well.

3 Likes

Thanks for sharing - I am running into the same issue! I notice the connection closes after just 1-2 minutes.

Mind sharing your patch? I added the ‘ping’ but got an error as the event type is unsupported, so figure it’s missing the ‘pong’

Also, what are you noticing in practice is a duration for a “long running monologue” that’s unsupported?

@hemlocket tagging in case it doesn’t notify

The maximum session length has been increased from 15 minutes to 30 minutes on the Realtime API beta.

Here’s the latest announcement:

1 Like

@hemlocket - I’d love to know more about your patch, I’m running into the same issue on my end.

I was having an issue like this. OpenAI’s websocket server sends pings with a 20 second keepalive, so if you don’t respond with pong before that it will disconnect. It sends an array of bytes in the ping and you have to pong with the same bytes.

I am facing similar issue. I am not using any openai client SDK, just using raw endpoint and nodejs ws library.

Even after implementing explicit pong replies at client side, still I randomly see some of my clients disconnecting with.

Disconnected from OpenAI code: 1011 reason: keepalive ping timeout

This is what I am doing at client side

 client.on('ping', (data) => {
        client.pong(data);
      });

@hemlocket @chrisacker would be great if you help the community please for solving this issue.