{"record":{"id":"7e5fd5c582740b73","repo":"gofr-dev/gofr","slug":"could-not-open-websocket-connection","errorCode":null,"errorMessage":"Could not open WebSocket connection","messagePattern":"Could not open WebSocket connection","errorType":"http","errorClass":null,"httpStatus":400,"severity":"error","filePath":"pkg/gofr/http/middleware/web_socket.go","lineNumber":21,"sourceCode":"import (\n\t\"context\"\n\t\"net/http\"\n\n\tgorillaWebsocket \"github.com/gorilla/websocket\"\n\n\t\"gofr.dev/pkg/gofr/container\"\n\t\"gofr.dev/pkg/gofr/websocket\"\n)\n\n// WSHandlerUpgrade middleware upgrades the incoming http request to a websocket connection using websocket upgrader.\nfunc WSHandlerUpgrade(c *container.Container, wsManager *websocket.Manager) func(inner http.Handler) http.Handler {\n\treturn func(inner http.Handler) http.Handler {\n\t\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\t\tif gorillaWebsocket.IsWebSocketUpgrade(r) {\n\t\t\t\tconn, err := wsManager.WebSocketUpgrader.Upgrade(w, r, nil)\n\t\t\t\tif err != nil {\n\t\t\t\t\tc.Errorf(\"Failed to upgrade to WebSocket: %v\", err)\n\t\t\t\t\thttp.Error(w, \"Could not open WebSocket connection\", http.StatusBadRequest)\n\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\t// Add the connection to the hub\n\t\t\t\twsManager.AddWebsocketConnection(r.Header.Get(\"Sec-WebSocket-Key\"), &websocket.Connection{Conn: conn})\n\n\t\t\t\t// Store the websocket connection key in the context\n\t\t\t\tctx := context.WithValue(r.Context(), websocket.WSConnectionKey, r.Header.Get(\"Sec-WebSocket-Key\"))\n\t\t\t\tr = r.WithContext(ctx)\n\t\t\t}\n\n\t\t\tinner.ServeHTTP(w, r)\n\t\t})\n\t}\n}\n","sourceCodeStart":3,"sourceCodeEnd":38,"githubUrl":"https://github.com/gofr-dev/gofr/blob/187eb24962502e91f1fee856230670958b66e89c/pkg/gofr/http/middleware/web_socket.go#L3-L38","documentation":"The WebSocket middleware returns HTTP 400 with the body 'Could not open WebSocket connection' when gorilla's Upgrade() fails while handling an upgrade request. This happens when the request does not meet WebSocket handshake requirements or the underlying connection cannot be hijacked. It is an HTTP response, not a Go error value.","triggerScenarios":"Client sends a WebSocket upgrade request missing required headers (Sec-WebSocket-Key, Sec-WebSocket-Version, valid Upgrade/Connection); request passes through a proxy that strips headers; response headers already written (HTTP/2 or buffering) preventing connection hijack.","commonSituations":"Browsers/clients behind corporate proxies that mangle upgrade requests; serving WebSockets over HTTP/2 or through servers that don't support hijacking; ad-blocking proxies interfering with the handshake.","solutions":["Ensure the client sends a valid RFC 6455 handshake (Upgrade: websocket, Connection: Upgrade, Sec-WebSocket-Key, Sec-WebSocket-Version: 13)","Serve the app over HTTP/1.1 and confirm the server/proxy supports connection upgrade; add proxy headers (e.g. proxy_set_header Upgrade/Connection in nginx)","Check server logs for the 'Failed to upgrade to WebSocket: %v' line to see the underlying upgrade error"],"exampleFix":"// before (nginx)\nlocation /ws { proxy_pass http://backend; }\n// after (nginx)\nlocation /ws {\n  proxy_pass http://backend;\n  proxy_http_version 1.1;\n  proxy_set_header Upgrade $http_upgrade;\n  proxy_set_header Connection \"upgrade\";\n}","handlingStrategy":"try-catch","validationCode":"if !gorillaWebsocket.IsWebSocketUpgrade(r) || r.Header.Get(\"Sec-WebSocket-Key\") == \"\" || r.Header.Get(\"Sec-WebSocket-Version\") != \"13\" { http.Error(w, \"invalid handshake\", http.StatusBadRequest); return }","typeGuard":null,"tryCatchPattern":"conn, err := upgrader.Upgrade(w, r, nil)\nif err != nil {\n    logger.Errorf(\"upgrade failed: %v\", err)\n    http.Error(w, \"Could not open WebSocket connection\", http.StatusBadRequest)\n    return\n}","preventionTips":["Serve WebSockets over HTTP/1.1 only; disable HTTP/2 for WS routes","Configure reverse proxies to forward Upgrade/Connection headers","Check server logs for the underlying upgrade error message"],"tags":["websocket","http","upgrade","network"],"backgroundTag":"websocket-handshake-failed","analyzedSha":"187eb24962502e91f1fee856230670958b66e89c","analyzedAt":"2026-09-01T20:34:54.554Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}