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, nil

View on GitHub (pinned to e064f32e36)

Solutions

  1. Disable compression by not setting dialer.EnableCompression = true and retry
  2. Upgrade to the latest gorilla/websocket version in case the negotiation bug is fixed
  3. 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

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


AI-assisted analysis of gorilla/websocket@e064f32e36 (2026-08-31). Data as JSON: /api/errors/abafd2cbf1d2f2ab. Report an issue: GitHub.