HTTP vs WebSocket

HTTP 500 Internal Server Error vs WebSocket 1011 Internal Error

HTTP 500 and WebSocket 1011 both indicate unexpected server-side errors, but in different communication models. HTTP 500 terminates a single request-response cycle, while WebSocket 1011 closes a long-lived bidirectional connection due to an internal server failure.

Descrição

The server encountered an unexpected condition that prevented it from fulfilling the request. A generic catch-all for server-side errors.

Quando você o vê

When an unhandled exception occurs, a database connection fails, or server code has a bug.

Como corrigir

Check server logs for the stack trace. Common causes: unhandled exceptions, database errors, misconfigurations.

Descrição

The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.

Quando você o vê

The server hit an unhandled exception or crashed while processing a WebSocket message. This is the WebSocket equivalent of HTTP 500 Internal Server Error.

Como corrigir

Check the server-side application logs for stack traces and exceptions. Fix the underlying bug that caused the crash and add proper error handling around message processing.

Diferenças principais

1.

HTTP 500 ends a single request; WebSocket 1011 terminates an entire persistent connection.

2.

WebSocket 1011 is sent as a close frame with the status code; HTTP 500 is a response status line.

3.

After HTTP 500, the client can immediately retry the request; after WebSocket 1011, the client must reconnect.

4.

WebSocket 1011 may cause data loss if the server crashes mid-stream; HTTP 500 is atomic per request.

5.

Both indicate a bug or unexpected condition that the server could not handle gracefully.

Quando usar qual

Return HTTP 500 for unexpected errors in request-response APIs. Send WebSocket close code 1011 when the server must terminate a WebSocket connection due to an unexpected error like an unhandled exception or a backend dependency failure. Clients should implement automatic reconnection with backoff when they receive 1011.

Saiba mais