socket.io connection via proxy url on Dreamhost VPS generates bad response

59 views Asked by At

This may be a question specific to the Dreamhost VPS environment, or perhaps a more general problem. I have a node.js server running on my Dreamhost VPS. Instead of socket.io in the client connecting directly to the port that node is running on, I am trying to connect via the proxy url that uses https. The problem is that Dreamhost does not allow editing the https.conf file, so I have to use their proxy service. Currently, the apache proxy url https://example.net/simpleserver serves up an index page from the node server on port 8021. So for web serving, the proxy is working.

But when I try to connect to the the server via socket.io, I get an error:

"WebSocket connection to 'wss://example.net/simpleserver/socket.io/?EIO=4&transport=websocket' failed: There was a bad response from the server."

The SimpleServer.js code is:

const http = require('http');

const app = express();
const server = http.createServer(app);

const socketIo = require('socket.io');
const io = socketIo(server, { path: '/simpleserver/socket.io/' });


app.get('/', (req, res) => {
    
    console.log("server.js: index page server.");
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.write("Welcome to SimpleServer!!! ");
    res.end();

});



// Set the server to listen on port 8021
const port = 8021;
server.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

And the client code is:

<head>
  <title>Simple Chat</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.1.3/socket.io.js"></script>

</head>
<body>
  <ul id="messages"></ul> --
  <input id="messageInput" autocomplete="off" /><button id="sendButton">Send</button>

  <script>
      
    // Connect to the WebSocket server
    const socket = io.connect('https://example.net', {path: '/simpleserver/socket.io/', transports: ["websocket"]});

  </script>
</body>
</html>

Has anyone been able to connect websockets via a proxy on Dreamhost VPS?

0

There are 0 answers