40

I have set up a node server with socket io turning and trying to connect to it through another server. However some browsers on different computers give me this error and makes it reconnect the whole time:

XMLHttpRequest cannot load https://serverDomain.net:3000/socket.io/?EIO=3&transport=polling&t=Lo_SdiU. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'https://www.differentServerDomain.fr' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

my js config:

var port = 3000;
var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();

var options = {
    key: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/privkey.pem'),
    cert: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/fullchain.pem')
};
var server = https.createServer(options, app);
var io = require('socket.io')(server);
io.origins('https://www.differentServerDomain.fr:* https://www.differentServerDomain.fr/wp-admin/index.php:*');

// start of server
server.listen(port, function(){
    console.log('listening on *: '+ port + "\n");
});

I am using node 8.0 and socket io 2.2, Your help will be greatly appreciated.

EDIT: here is the client code:

<script src="https://serverDomain.net:3000/socket.io/socket.io.js"></script>
<script>
   var socket = io('https://serverDomain.net:3000');
</script>
lolplayer101
  • 2,112
  • 2
  • 17
  • 26

9 Answers9

116

I have found a solution. for some reason the default transportation method is not always allowed by all servers.

So i specified a neutral transportation method at the client side, like this:

var socket = io('https://yourDomain:3000', { transports : ['websocket'] });
Davide Pizzolato
  • 679
  • 8
  • 25
lolplayer101
  • 2,112
  • 2
  • 17
  • 26
43

This worked for me

var socket = io('http://yourDomain:port', { transports: ['websocket', 'polling', 'flashsocket'] });
Ahmed Sayed
  • 431
  • 4
  • 3
25

For users of Socekt io v3

You need to use option credentials in cors options of server config.

Example:

const io = require('socket.io')(strapi.server, {
  cors: {
    origin: "http://localhost:3000",
    credentials: true
  }
});

Docs:

https://expressjs.com/en/resources/middleware/cors.html https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin

Related topic:

Socket.io + Node.js Cross-Origin Request Blocked

Migration guide

https://socket.io/docs/v3/migrating-from-2-x-to-3-0/index.html#CORS-handling

Daniel
  • 7,684
  • 7
  • 52
  • 76
12

In Client code add this, I got error "NO Access-Control-Allow-Origin' header is present". so added this on client side.

var connectionOptions =  {
            "force new connection" : true,
            "reconnectionAttempts": "Infinity", 
            "timeout" : 10000,                  
            "transports" : ["websocket"]
        };

        this.socket = io.connect('http://localhost:5000',connectionOptions);
Wasi Sharief
  • 159
  • 1
  • 5
7

Try this, it worked for me:

const io = require("socket.io")(httpServer, {
  cors: {
    origin: "http://localhost:8080",
    methods: ["GET", "POST"]
  }
});
marko-36
  • 1,309
  • 3
  • 23
  • 38
Ujjwal Rawat
  • 71
  • 1
  • 1
7

I recently faced the same issue and the solution that worked for me was the following:

//in the React project install socket.io-client and then

import io from 'socket.io-client'   

var socket = io('http://localhost:<your_port_number>', {transports: ['websocket', 'polling', 'flashsocket']});
Aaron Gomes
  • 71
  • 1
  • 2
6

Specify the following on the client-side.

const socket = io('https://yourDomain:3000', { transports: ['websocket'] });

instead of const socket = io('https://yourDomain:3000');

1

Hope this modification will help you.

    var port = 3000;
    var fs = require('fs');
    var https = require('https');
    var express = require('express');
    var app = express();

    app.use(function(req, res, next) {
      res.header("Access-Control-Allow-Origin", "https://www.differentServerDomain.fr https://www.differentServerDomain.fr");
      res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
      next();
    });


    var options = {
        key: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/privkey.pem'),
        cert: fs.readFileSync('/etc/letsencrypt/live/devpeter.net/fullchain.pem')
    };
    var server = https.createServer(options, app);
    var io = require('socket.io')(server);

    // start of server
    server.listen(port, function(){
        console.log('listening on *: '+ port + "\n");
    });
M.Navy
  • 155
  • 7
  • 1
    didn't chnage anything, keeps disconnecting reconecting with the same XMLHttpRequest error. thank you for trying. – lolplayer101 Jun 19 '17 at 13:51
1

Also please check server-side log having this msg "info - unhandled socket.io url" or not. if shown, please upgrade your socket.io version to 1.0+. I solved it by this approach.

npm install socket.io@1.0

or change package.json like that:

"dependencies": {
    "socket.io": "^1.0"
}
licaomeng
  • 919
  • 2
  • 13
  • 27
  • thank you for your answer licaomeng, but i have a few functions relying on the 2.0+ version of socket io. in any case, explicitly declaring the transportation method of the protocal to websocket did it for me (see the accepted answer). – lolplayer101 Apr 05 '18 at 18:54