I am using socket.io as the websocket lib to do some communication. This is the socket.io(v4) server side code look like:
// websocket
const websocketServer = new Server(httpServer, {
cors: {
origin: [";, "*"],
credentials: true,
allowedHeaders: ["*"],
methods: ["GET", "HEAD", "OPTIONS","POST"],
},
path: "/socket.io/"
});
and this is the client side config code look like:
let options: Partial<ManagerOptions & SocketOptions> = {
withCredentials: true,
path: "/socket.io/"
};
but the communication shows invalid namespace error:
44/d2f240bc84954f59af1bd1be22113373,{message: "Invalid namespace"}
message
:
"Invalid namespace"
this is the http command look like:
curl '/socket.io/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEwMywiZGV2aWNlSWQiOiJmZjY1YWJjZWNkYjQ1YmNjZThmYTJkZThiZDEwZWVjYiIsImFwcElkIjoibjI5UGEyOVdTMSIsImx0IjoxLCJldCI6MCwicGlkIjoxMywiZXhwIjoxNzM5MTE3ODMzfQ.1IVMKbc9y8NiTkN_fAetq8L1Pob91fmvbOxG4WqtihQ&from=web_tex_editor&EIO=4&transport=polling&t=qbrfm94f&sid=yyA8lfOrThCzvjcFAAAE' \
-H 'Accept: */*' \
-H 'Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,zh-TW;q=0.7,fr;q=0.6' \
-H 'Connection: keep-alive' \
-H 'DNT: 1' \
-H 'Origin: ' \
-H 'Referer: /' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: same-site' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
-H 'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'sec-ch-ua-platform: "macOS"'
Am I missing something? what should I do to fixed this issue? I have tried to add the sticky session config in nginx:
server {
listen 443 ssl;
server_name socket.example.top;
ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example/privkey.pem;
if ($allowed = 0) {
return 403;
}
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://nodes;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
location /socket.io/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://nodes;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
}
upstream nodes {
# enable sticky session with either "hash" (uses the complete IP address)
hash $remote_addr consistent;
server 127.0.0.1:8000;
}
also tried to add annotation in kubernetes service:
apiVersion: v1
kind: Service
metadata:
name: tex-socketio-service
namespace: reddwarf-pro
uid: 2a19e6ad-2516-4cff-b854-de3e0d760f7f
resourceVersion: '34727656'
creationTimestamp: '2025-02-03T08:54:25Z'
labels:
k8slens-edit-resource-version: v1
annotations:
traefik.ingress.kubernetes.io/affinity: "true"
traefik.ingress.kubernetes.io/load-balancer-method: drr
the net path is: user->nginx->traefik(in kuberentes)->service->deployment-pod.