gorilla/websocket · error
websocket: invalid compression negotiation
Error message
websocket: invalid compression negotiation
What it means
errInvalidCompression is an internal error returned by DialContext when the permessage-deflate extension negotiation produces an invalid result. It means the client and server agreed on compression in a way the library cannot honor. It is unexported; callers cannot match it directly.
Source
Thrown at client.go:26
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strings"
"time"
)
// ErrBadHandshake is returned when the server response to opening handshake is
// invalid.
var ErrBadHandshake = errors.New("websocket: bad handshake")
var errInvalidCompression = errors.New("websocket: invalid compression negotiation")
// NewClient creates a new client connection using the given net connection.
// The URL u specifies the host and request URI. Use requestHeader to specify
// the origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies
// (Cookie). Use the response.Header to get the selected subprotocol
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie).
//
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a
// non-nil *http.Response so that callers can handle redirects, authentication,
// etc.
//
// Deprecated: Use Dialer instead.
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) {
d := Dialer{
ReadBufferSize: readBufSize,
WriteBufferSize: writeBufSize,
NetDial: func(net, addr string) (net.Conn, error) {
return netConn, nilView on GitHub (pinned to e064f32e36)
Solutions
- Disable compression by not setting dialer.EnableCompression = true and retry
- Upgrade to the latest gorilla/websocket version in case the negotiation bug is fixed
- Fix or report the server's Sec-WebSocket-Extensions response to conform to RFC 7692
Example fix
// before
dialer := websocket.Dialer{EnableCompression: true}
// after (workaround for non-conformant server)
dialer := websocket.Dialer{} // compression disabled Defensive patterns
Strategy: fallback
Try / catch
conn, _, err := dialer.DialContext(ctx, url, nil)
if err != nil {
// errInvalidCompression is unexported; fall back to a non-compressed dial
dialer.EnableCompression = false
return dialer.DialContext(ctx, url, nil)
} Prevention
- Only enable compression with servers known to implement RFC 7692 correctly
- Keep gorilla/websocket updated so negotiation fixes are picked up
- Prefer default (no compression) unless payload sizes justify it
When it happens
Trigger: Calling DialContext with EnableCompression set to true when the negotiated compression extension parameters from the server response are invalid (e.g. server echoes extension parameters outside what the client offered, like an unexpected context takeover or no-context-takeover value).
Common situations: Dialing a non-conformant server that echoes back malformed Sec-WebSocket-Extensions parameters during compression negotiation; rare in practice, mostly seen with custom or buggy server implementations.
Related errors
- websocket: bad handshake
- websocket: internal error, unexpected bytes at end of flate
- websocket: invalid compression level
- http.StatusText(status)
- malformed ws or wss URL
AI-assisted analysis of gorilla/websocket@e064f32e36 (2026-08-31).
Data as JSON: /api/errors/abafd2cbf1d2f2ab.
Report an issue: GitHub.