socket.io-client icon indicating copy to clipboard operation
socket.io-client copied to clipboard

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Open Neumann-Valle opened this issue 12 years ago • 108 comments

XMLHttpRequest cannot load http://domain.com:8080/socket.io/?EIO=2&transport=polling&sid=z1N_9JpKElJJ6b2qAAAL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://domain.com' is therefore not allowed access. server: var server = require('http').Server(); var io = require('socket.io')(server); io.on('connection', function(socket){ socket.on('test', function(data){}); socket.on('event', function(data){}); socket.on('disconnect', function(){}); }); server.listen(8080);

client: var socket = io.connect('http://domain.com:8080'); socket.on('connect', function(){ socket.emit( 'test' , { test : 'right' });// if use right on connect , errors out socket.on('event', function(data){}); socket.on('disconnect', function(){}); });

Can anyone replicate this issue? browser giving error is Chrome, in Firefox just loops.. I think is because when connect and you try to send an emit right away , the client is upgrading connection and fired the emit every time..

It just an assumption , one with better acknowledge could give some more info..

Same test was done with previous repository ver 1.. and it doesn't have same behavior.

Neumann-Valle avatar Mar 05 '14 19:03 Neumann-Valle

Sorry can't post client side code properly, github just blank it out.. Ok , client side test code can be seen.. Anyone could reproduce?

Neumann-Valle avatar Mar 05 '14 19:03 Neumann-Valle

So this isn't important, I passed two days debugging my code to see what I was doing wrong.. This behavior doesn't happens in socket.io ver 0.9.16 and older..

Neumann-Valle avatar Mar 05 '14 21:03 Neumann-Valle

I'm experiencing the same issue:

//server
var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(8080);

function handler (req, res) {
  console.log('handler');
  fs.readFile(__dirname + '../index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}
io.set( 'origins', '*localhost:8080' );

io.sockets.on('connection', function (socket) {
  socket.on('join room', function (data) {
    console.log(data);
    console.log('join');
    socket.emit('user joined', {});
  });
});


//client
socket = io.connect('http://localhost:8080/'); 
_room = room;

console.log('connecting user to socket server');

socket.on('error', function() {
  console.log('there was an error');
});

socket.on('user joined', function(data) { 
  if (data.user_count == 2) {   
    console.log('users are connected');
    users_connected_handler();
  }
});

socket.on('webrtc', function(data){ 
  message_received_handler(data.data);  
});

socket.on('connect', function() {
  console.log('socket connection established');
  console.log("connecting to " + _room)
  socket.emit('join room', {room: _room});
  connected = true;     
});

BartlomiejSkwira avatar Mar 20 '14 15:03 BartlomiejSkwira

I tried to set the header, but it didin't help

function handler (req, res) {
  console.log('handler');
  fs.readFile(__dirname + '../index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }
    res.header('Access-Control-Allow-Origin', 'http://localhost:8080/');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
      res.send(200);
    }
    else {
      next();
    }
    res.end(data);
  });
}

BartlomiejSkwira avatar Mar 20 '14 15:03 BartlomiejSkwira

I see in your server code , io.set() , are you using current socket,io stable 0.9.16? I only have the issue in socket.io ver1 , haven't test socket.io ~1.0.0-pre but will test to see if still happens..

I found out if you emit right after connect in client you get this error...

Neumann-Valle avatar Mar 20 '14 15:03 Neumann-Valle

And your client code is emitting right after connect.. there is the issue.

Neumann-Valle avatar Mar 20 '14 15:03 Neumann-Valle

I'm on node 0.10.24 and latest socket.io.js from https://github.com/LearnBoost/socket.io-client

Nope, I commented out all emits, same issue: XMLHttpRequest cannot load http://localhost:8080/socket.io/?EIO=2&transport=polling. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4000' is therefore not allowed access.

BartlomiejSkwira avatar Mar 21 '14 09:03 BartlomiejSkwira

Issue for me, as well. I want to emit immediately after connection, which is before the transport is upgraded from XHR Polling.

Instructing the client to use the "websocket" transport first might help mitigate the issue, but it still won't help users in browsers that don't support websockets.

var socket = io('http://localhost', {transports: ['websocket', 'polling', 'flashsocket']});

engine.io-client XHR transport really needs CORS support.

twhitbeck avatar Mar 21 '14 13:03 twhitbeck

I'm also having this isue. socket.io-client 0.9.16, server is hosted on heroku.

danpe avatar Apr 15 '14 20:04 danpe

I'm also having this issue. socket.io-client 0.9.x I'am using Chronium and Firefox on Ubuntu 12.04

Solidary avatar Apr 24 '14 08:04 Solidary

good ^^*

Linrstudio avatar Apr 30 '14 06:04 Linrstudio

https://github.com/theoctal/livenote/blob/master/app.js#L11 look here. The issue is essentially solved by setting middleware for express. This makes good example for those who want to know about how to set middleware.

adityapatadia avatar Apr 30 '14 08:04 adityapatadia

I guess the socket.io connection request doesn't go through the middlewares set on express.

In my case Access-Control-Allow-Origin is set by default as where the request is from. It just works.

I'm using 1.0.0-pre2

oddui avatar May 05 '14 01:05 oddui

Experiencing the same problem

gahula avatar May 29 '14 23:05 gahula

Look at my code, works perfectly.

adityapatadia avatar May 30 '14 03:05 adityapatadia

I solved my issue by removing the the transports options....

var express = require('express'); var app = express(); var server = require('http').Server(app); var io = require('socket.io')(server, {origins:'domain.com:* http://domain.com:* http://www.domain.com:*'}); var compress = require('compression'); var redis = require('socket.io-redis'); var uuid = require('node-uuid'); var geoip = require('geoip-lite'); var _ = require('underscore')._;

io.adapter(redis({ host: 'localhost', port: }));

server.listen(, '', function(){ console.log("Server up and running..."); });

........

gahula avatar May 31 '14 18:05 gahula

The problem I faced was, serving the client socket.io.js from a different location.

You can avoid this issue by serving the client js file from the same server where you are trying to connect to.

for example, my initial client code was this and it was throwing error

<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

once I modified it to this, it worked alright.

<script src="http://mydomain.com/socket.io/socket.io.js"></script>
var socket = io.connect('http://mydomain.com/');

And my server code is,

var express = require('express');
var app = express();
app.use(function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Headers", "Content-Type");
        res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
        next();
    });
var server = http.createServer(app);
io = socketio.listen(server, {log:false, origins:'*:*'});
... //io.connect and events below

avinashega avatar Jun 02 '14 12:06 avinashega

:+1: I run into this issue when include client socket.io.js from a different domain than the server... using [email protected]

ilovett avatar Jun 04 '14 23:06 ilovett

I'm also running into this issue on 1.0.4 when serving it from the socket.io CDN

austinh avatar Jun 05 '14 02:06 austinh

Me too. Tried:

  1. io.set('origins', ':'); io.set('transports',['websocket','flashsocket','htmlfile','xhr-polling','jsonp-polling']);
  2. express .use(function(req, res, next){}) All failed. Be screwed for two days, the only progress is guessing [email protected] has new bugs

ScrewChineseCommunist avatar Jun 05 '14 18:06 ScrewChineseCommunist