Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest possible wrong Socket.IO URL instead of CORS error #1979

Open
rauchg opened this issue Feb 3, 2015 · 6 comments
Open

Suggest possible wrong Socket.IO URL instead of CORS error #1979

rauchg opened this issue Feb 3, 2015 · 6 comments
Labels
enhancement New feature or request

Comments

@rauchg
Copy link
Contributor

rauchg commented Feb 3, 2015

A lot of people seem to be mistaking the error No 'Access-Control-Allow-Origin' header is present on the requested resource for lack of CORS support in Socket.IO.

This seems to be due to usually trying to initialize a socket to the wrong URL. The URL exists and responds, but obviously it doesn't set CORS headers. And even if it did, it wouldn't be where the socket.io server is hosted.

Maybe we could console.warn in these situations to warn the developer about a potential URL error.

@Neumann-Valle
Copy link

+1

@morenoh149
Copy link

would be useful

@b4dnewz
Copy link

b4dnewz commented Nov 17, 2017

@rauchg can you explain a little bit the situation? I'm having this cors trouble and I can't find a solution to it.

I'm using express and socket.io, on express side I've cors package which enable cors for all domains, than on socketio i'm using the origin: '*:*' to do the same, but I still get the same CORS error.

The express/socket server is on a different subdomain than the client but all normal requests work pretty good but when it comes to socketio it doesn't set the headers.

Failed to load api.domain.com: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin app.domain.com is therefore not allowed access. The response had HTTP status code 503.

@rnnyrk
Copy link

rnnyrk commented Apr 2, 2019

Having the same issue, can't find a fix. anyone any idea?

@darrachequesne darrachequesne added the enhancement New feature or request label Nov 24, 2021
darrachequesne added a commit to socketio/engine.io-client that referenced this issue Apr 11, 2022
The close event will now include additional details to help debugging
if anything has gone wrong.

Example when a payload is over the maxHttpBufferSize value in HTTP
long-polling mode:

```js
socket.on("close", (reason, details) => {
  console.log(reason); // "transport error"

  // in that case, details is an error object
  console.log(details.message); "xhr post error"
  console.log(details.description); // 413 (the HTTP status of the response)

  // details.context refers to the XMLHttpRequest object
  console.log(details.context.status); // 413
  console.log(details.context.responseText); // ""
});
```

Note: the error object was already included before this commit and is
kept for backward compatibility

Example when a payload is over the maxHttpBufferSize value with
WebSockets:

```js
socket.on("close", (reason, details) => {
  console.log(reason); // "transport close"

  // in that case, details is a plain object
  console.log(details.description); // "websocket connection closed"

  // details.context is a CloseEvent object
  console.log(details.context.code); // 1009 (which means "Message Too Big")
  console.log(details.context.reason); // ""
});
```

Example within a cluster without sticky sessions:

```js
socket.on("close", (reason, details) => {
  console.log(details.context.status); // 400
  console.log(details.context.responseText); // '{"code":1,"message":"Session ID unknown"}'
});
```

Note: we could also print some warnings in development for the "usual"
errors:

- CORS error
- HTTP 400 with multiple nodes
- HTTP 413 with maxHttpBufferSize

but that would require an additional step when going to production
(i.e. setting NODE_ENV variable to "production"). This is open to
discussion!

Related:

- socketio/socket.io#3946
- socketio/socket.io#1979
- socketio/socket.io-client#1518
@sugar258596
Copy link

There is still such a problem, and it seems that it has not been solved

@darrachequesne
Copy link
Member

@sugar258596 the problem is that I don't think we can really tell a CORS issue from a network issue, since the preflight requests are automatically sent by the browser.

The XMLHttpRequest object does not give much information:

XMLHttpRequest {
  onabort: null
​  onerror: null
​  onload: null
​  onloadend: null
​  onloadstart: null
​  onprogress: null
​  ontimeout: null
​  readyState: 0
​  response: ""
​  responseText: ""
​  responseType: ""
​  responseURL: ""
​  responseXML: null
​  status: 0
​  statusText: ""
​  timeout: 0
​  withCredentials: false
}

status: 0 might mean different things:

  • Illegal cross origin request
  • Firewall block or filtering
  • The request itself was cancelled in code
  • An installed browser extension is mucking things up

See also: https://stackoverflow.com/questions/872206/what-does-it-mean-when-an-http-request-returns-status-code-0

Open to discuss about this though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants
@rauchg @morenoh149 @Neumann-Valle @rnnyrk @darrachequesne @b4dnewz @sugar258596 and others