Hey everyone,
I’m building a voice agent using the Realtime API with SIP (via Telnyx) and running into an intermittent issue that I’m trying to understand better.
Setup:
-
Incoming calls via Telnyx SIP → OpenAI Realtime API
-
Call acceptance works fine
-
I connect to the monitoring WebSocket at
wss://api.openai.com/v1/realtime?call_id=...
The issue: About 5% of the time, when I try to connect to the monitoring WebSocket, I get:
{"level":50,"time":1764788782041,"pid":673,"hostname":"d8d3736f251e48","err":{"type":"Error","message":"Unexpected server response: 521","stack":"Error: Unexpected server response: 521\n at ClientRequest.<anonymous> (/app/node_modules/ws/lib/websocket.js:913:7)\n at ClientRequest.emit (node:events:524:28)\n at HTTPParser.parserOnIncomingClient (node:_http_client:702:27)\n at HTTPParser.parserOnHeadersComplete (node:_http_common:118:17)\n at TLSSocket.socketOnData (node:_http_client:544:22)\n at TLSSocket.emit (node:events:524:28)\n at addChunk (node:internal/streams/readable:561:12)\n at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)\n at Readable.push (node:internal/streams/readable:392:5)\n at TLSWrap.onStreamRead (node:internal/stream_base_commons:191:23)"},"callId":"e7d4d67e-5168-4b79-85aa-4d15f30f365b","msg":"❌ Call monitor error"}
The call fails immediately and the caller hears nothing. When I retry the same call a few seconds later, it works perfectly.
From what I understand, 521 is a Cloudflare error indicating the origin server refused the connection, which suggests it might be a transient capacity issue rather than something wrong with my setup.
Has anyone else experienced this, and what’s the recommended approach for handling these connection failures in production? preparing to demo this to customers and want to make sure I’m handling this gracefully. many thanks