---
"@context": https://schema.org
"@type": TechArticle
"@id": https://www.twilio.com/docs/api/errors/31921#article
headline: "31921: Stream - WebSocket - Close Error"
url: https://www.twilio.com/docs/api/errors/31921
inLanguage: en
dateModified: 2026-06-08T11:38:30.000Z
author:
  "@type": Organization
  name: Twilio Developer Education Team
publisher:
  "@type": Organization
  name: Twilio
---

# 31921: Stream - WebSocket - Close Error

Log Type: APPLICATION

Log Level: ERROR

## Description

This error occurs when Twilio establishes a Media Streams WebSocket connection to your server and your server then closes that connection. In bidirectional Media Streams, closing the WebSocket from your server stops the Stream.

### Possible causes

* Your WebSocket server ended the connection and Twilio received a WebSocket close event.
* Your application closed the WebSocket for a bidirectional Stream started with `<Connect><Stream>`. Twilio resumes the remaining TwiML only after your server closes the WebSocket connection.
* The WebSocket connection associated with the Media Stream was closed by your server before you intended the Stream to end. Each Media Stream uses a single WebSocket connection.

### Possible solutions

* Review your WebSocket server logs to identify the close code and the condition that caused the server to end the connection.
* If you use bidirectional Media Streams with `<Connect><Stream>`, keep the WebSocket connection open for as long as you want the Stream to continue. Close it only when you intend to stop the Stream.
* Add a `statusCallback` URL to `<Stream>` so Twilio sends `stream-started`, `stream-stopped`, and `stream-error` events. Use the `StreamError` value in that callback to capture more detail about the failure.
* Make sure your Media Streams application accepts secure WebSocket connections from Twilio on TCP port 443.

#### Additional resources

* [Media Streams Overview](/docs/voice/media-streams)
* [WebSocket Messages for Media Streams](/docs/voice/media-streams/websocket-messages)
* [TwiML `<Stream>` Reference](/docs/voice/twiml/stream)
