Ewan Sou
Ewan Sou

Reputation: 197

Client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header

I am using Gorilla Websocket package to implement a websocket.

conn, err := upgrader.Upgrade(w, r, nil)

    if err != nil {
        // handle error
        fmt.Println(err)
    }
    defer conn.Close()

I am see the below error

websocket: the client is not using the websocket protocol: 'upgrade' token not found in 'Connection' header

I printed on the header of my request, and I am seeing the below

Sec-Fetch-User ?1
Sec-Fetch-Dest document
Referer http://localhost:4747/home
Cookie myGoLiveCookie=369d99fa-901d-4b23-a64b-4731247de304
Sec-Ch-Ua "Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"
Sec-Ch-Ua-Mobile ?0
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
Sec-Fetch-Site same-origin
Accept-Encoding gzip, deflate, br
Upgrade-Insecure-Requests 1
Accept text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Mode navigate
Accept-Language en-GB,en-US;q=0.9,en;q=0.8
Connection keep-alive

There is no Upgrade websocket or Connection Upgrade as per expected

I believe I am facing the exact same issue as this one.

Upvotes: 14

Views: 28500

Answers (3)

KIRAN KUMAR B
KIRAN KUMAR B

Reputation: 538

If you are deploying the WS server in a VM and want this header to be there by default, update your Nginx config file like:

location /ws/ {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_set_header Upgrade websocket;
        proxy_set_header Connection Upgrade;
        proxy_pass         "http://127.0.0.1:8089";
    }

proxy_set_header Upgrade websocket; proxy_set_header Connection Upgrade; will do the job for you even if the client has not passed the headers.

Upvotes: 10

Aman Agarwal
Aman Agarwal

Reputation: 444

the error has arrived because you called /ws/:roomId manually, this endpoint is hit internal from the frontend. you can go to the index.html file and print console.log(roomId) after this endpoint is hit from frontend, and that will conclude the error!

Upvotes: 0

eudore
eudore

Reputation: 753

The browser js request ws connection method is wrong, the correct ws request code var ws = new WebSocket("ws://localhost:4747/ws");.

A correct ws request header, each header in it is necessary, but the value is different.

GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==
Sec-WebSocket-Protocol: chat, superchat
Sec-WebSocket-Version: 13
Origin: http://example.com

Upvotes: 3

Related Questions