node.jssocket.iocorsengine.io

Socket IO allows CORS request from curl, but not from client application


I am making an application in which i have a node backend, and an angular frontend.

I am using socket.IO to communicate between my client and server.

I was facing CORS issue, which i solved (tried to) as:

const io: SocketIOServer = new SocketIOServer(server, {
  cors: {
    origin: true
  }
}); // only for development

But still, upon making my request from my client app (running in another port) gives me CORS issue.

So i went to my terminal, and made a request with curl,as shown here:

me@Desktop:~$ curl "http://127.0.0.1:5000/socket.io/?EIO=4&transport=polling&t=NV5sBAn"
0{"sid":"jAhPIEEkdy8EY8I_AAAD","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}

This suggests me that curl is being able to access the server, and is not facing CORS issue.

In my client, i am trying to connect to my server as:

socket = io.connect('http://locahost:5000'); // server running at port 5000

Help me with my issue, so i can connect with my server.

Also on a sidenote: If the version of server.io in the app is 3, the curl request to the server is also failing. only upon server.io version 4, the curl request is passing.

The error in firefox if that helps:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://locahost:5000/socket.io/?EIO=4&transport=polling&t=NV5vi1_. (Reason: CORS request did not succeed).

EDIT: my issue is not being able to connect to the server with client. I showed the curl because it was suggested in the socket.io cors configuration webpage.


Solution

  • Apprrently, the issue was:

     socket = io.connect('http://localhost:5000');
    

    changing this to:

     socket = io.connect('http://127.0.0.1:5000');
    

    worked!! I have no idea why this name was not getting resolved!!