7

Hei, i am trying to use socket.io with cross domains. For example lets say domains are: https://client.domain.com and https://server.domain.com. My client side code looks like this:

socket = io.connect("https://server.domain.com");
socket.on("connect", function () {
    console.log("socketio Connected to server!");
});
socket.emit("test", {"test":"test"});

And server side:

var fs = require('fs');
var https = require('https');
var express = require('express');
var socketIO = require('socket.io');
var app = express();

// Settings
var serverOptions = {
  key: fs.readFileSync('privkey1.pem'),
  cert: fs.readFileSync('cert1.pem')
};

var serverPort = 443;

// Logic
var server = https.createServer(serverOptions, app);
var io = socketIO(server);

io.on('connection', function(socket) {
  socket.on("test", function(data) {
    console.log(data);
  });
});

server.listen(serverPort, function() {
  console.log('server up and running at %s port', serverPort);
});

Everything works, messages get sent back and forth. But some reasone socket.io keeps trying to use polling and on wrong domain. Almost every second i see these request:

Request URL:https://client.domain.com/socket.io/?EIO=3&transport=polling&t=1452418594321-145
Request Method:GET
Status Code:302 OK

Why is socket.io doing that and how can i turn this off?

EDIT: whats more interesting is that if i comment out all the code to do with socket.io and only leave in the <script src="https://server.domain.com/socket.io/socket.io.js"></script> it still keeps making these HTTP requests described earlier.

EDIT2: Not sure if this is going to make any difference but in reality the domains are not subdomains of one domain. They are https://clientdomain.com and https://serverdomain.com.

hs2d
  • 6,027
  • 24
  • 64
  • 103

2 Answers2

0

If you're using nginx, I think your issue come from the way you configured it. You absolutely need the following things in your location block as stated in the socket.io docs

Make sure you got these headers set correctly:

location / {
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $host;

  ...
}

Socket.IO handle pretty well CORS, so I doubt the problem relies here.

Preview
  • 35,317
  • 10
  • 92
  • 112
0

The request that you are seeing is because you are serving the client from https://client.domain.com.

The

Request URL:https://client.domain.com/socket.io/?EIO=3&transport=polling&t=1452418594321-145

is just the client getting the files for socket.io so that it can use the io object. This is not related to the connection to https://server.domain.com because it is the request of the socket.io files that are being served from https://client.domain.com.

Your code is connecting a client to https://server.domain.com using the socket.io source files it recieved from https://client.domain.com through https//server.domain.com. Once the files are received from the server, they are served to the client via its connection to https://client.domain.com.

Essentially, the request is just the code's way of saying that it's requesting the socket.io files from the client domain, which originated from the server domain but are sent through the client domain to the actual client (not the client domain).

Blubberguy22
  • 1,344
  • 1
  • 17
  • 29