Common session errors and closures
In WebSocket based connections, closures and errors represent different ways a connection can terminate. A closure is a normal, expected termination initiated by either the client or the server, whereas errors are terminations resulting from an unexpected problem like network issues, protocol mismatches, timeouts, or server-side issues. In the event of an error, theon_error callback is triggered just prior to on_close. If an error is not encountered, then only on_close is called.
When a session closes, the on_close callback receives a status code and reason detailing why the connection ended.
This information is useful when attempting to debug issues or handle certain closure scenarios programmatically.
The below table lists some of the common reasons for a session closure along with their corresponding codes and descriptions.
The Error message
When a session ends because of an error (rather than a normal closure), the server sends an Error text frame immediately before it closes the WebSocket. The frame carries the close code and a human-readable message:
When the server sends the
Error frame, the close reason is always See Error message for details, whatever the length of the error, and the full text is in the Error frame above. The close reason carries the error text itself, cut to 123 bytes (the most a close frame can hold), only if the Error frame could not be sent. Read the Error message rather than relying on the close reason.Close codes
The Error text column shows theerror field of the Error message that comes before the close.
Deciding whether to retry a
1008Code 1008 covers both the streaming rate limit and credential or account problems, so the code alone does not tell you whether retrying can help. Decide from the error text of the Error message: retry with backoff only on Unauthorized Connection: Too many concurrent sessions, in the next minute. For any other 1008 text, fix the key, token or account first; reconnecting unchanged fails again.Code
3005 is still used as a catch-all for server-side errors not covered by
the more specific codes above.410 on the upgrade request. This is an HTTP status, not a WebSocket close code. Use this endpoint instead: wss://streaming.assemblyai.com/v3/ws.
Handling closed sessionsA common way to handle a closure such as
3008 - Session Expired: Maximum session duration exceeded is to parse the status code and reason in the on_close callback. If a specific code and reason are detected, you can then take appropriate action, such as opening a new session or logging useful debugging information.Note that the on_error callback is not triggered in this case, as the session closes for a known reason and not due to encountering an error.