Socket.io Engine.io problems "?EIO=4&transport=polling&t=OUAHy-a 404"

3.4k Views Asked by At

When I try to connect the client to my website with socket.io, it just spams https://example.com/socket.io/?EIO=4&transport=polling&t=OUAHy-a 404 (At other times the request has just timed out instead of 404) repeatedly in the client console. Here is the client html:

<!DOCTYPE html>
<html>
  <head>
    <title>Zesty testy</title>
  </head>
  <body>
    <script src="https://cdn.socket.io/4.4.1/socket.io.js"></script>
    <script src="./client.js"></script>
  </body>
</html>

And here is the client js:

const socket = io();

At last, here is the server js:

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server);

io.on('connection', (socket) => {
  console.log('a user connected');
});

server.listen(3000, () => {
  console.log('listening on *:3000');
});

I have looked at many other questions alike mine, but no answer has been able to change the outcome. Most of the code is from the socket.io webpage, with some slight change. I have been trying to get this to word for so long, and I will happily answer any questions to try and get this work. Thanks! (I am not using localhost.)

The expected result is for the Engine.io request thing to have status code 200 instead of 404, and the server console to display that a user has connected.

Edit: I have now some further knowledge about my error, and I now know that adding " cors: origin="*" " doesn't work. Though I have seen one video about this that mentioned a supposed way to fix, this, my hosting service that I use doesn't seem to have that as an option for me. Still not solved!

1

There are 1 best solutions below

18
Quentin On

You have two HTTP servers.

  1. Some unspecified server that is providing https://example.com/ and is hosting your HTML and client-side JavaScript
  2. The server that is created when you run server.js with Node.js (assuming you are running it at all).

The io function will, by default, try to connect to a socket.io instance on the same origin. Since your HTML document is running on 1 it tries to connect with socket.io to 1.

Since your server which actually provides the socker.io service is 2 this results in a 404 Not Found error.

As per the documentation, you need to specify the URL to server 2 so that you can connect to the socket.io service you've created.

 const socket = io("http://whereever.example.com:3000");

Note that you might hit problems trying to reach an unsecured (i.e. non-HTTPS) socket.io service from a secured origin.