Home > Enterprise >  Socket.io client stuck at 101 and keeps polling using xhr
Socket.io client stuck at 101 and keeps polling using xhr

Time:09-13

This problem is driving me crazy.

I have this machine using express and socket.io over ssl (using nginx proxy pass)

const { v4: uuidV4 } = require('uuid')
const express = require('express')
const app = express()
const fs = require('fs')
const https = require('https')
const options = {
  key: fs.readFileSync('/etc/ssl/xxx/ssl.key'),
  cert: fs.readFileSync('/etc/ssl/xxx/ssl.combined'),
}
const httpsServer = https.createServer(options, app)
httpsServer.listen(3000, function () {
  console.log("Example app listening at %s",  httpsServer.address().port);
});

const io = require("socket.io")(httpsServer);

Everything seems to work, if I visit the url, the messages are working fine. But in reality it work only over polling and if I force transports: ['websocket'] I get 101 "switching protocol" but I don't get any upgrade.

This is my nginx conf for that domain.

location / {
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass https://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    listen 161.97.166.96:443 ssl;
    ssl_certificate /etc/ssl/xxx/ssl.combined;
    ssl_certificate_key /etc/ssl/xxx/ssl.key;

Any help?

CodePudding user response:

In the end I had to create another server on another port and worked without a problem.

const server2 = https.createServer(options)
server2.listen(3002)
const io = require("socket.io", { secure: true, transports: ["websocket"] })(server2);

I also had to update my nginx conf

   location /socket.io/ {
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass https://127.0.0.1:3002;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
  • Related