2

I'm trying to enable Cross-Origin Same Domain requests for my socket.io nodejs server, but for some reason it keeps telling me io.origins is not a function ?

var io = require('socket.io', 3000);
io.origins("*");

TypeError: io.origins is not a function

**UPDATE, The Cross-Origin Error: **

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

Ran Shorowitz
  • 307
  • 5
  • 11

2 Answers2

2

EDIT 2: As for why you're getting that CORS error, it's because you haven't configured the Socket.IO cors option properly.

Now, the cors configuration for Socket.IO actually gets passed to the NPM package cors, so you need to make your cors configuration work with the cors package.

For your case, that would look like:

cors: {
    // The `*` is used as the wildcard here.
    origin: "*",
    // Set the other options according to your needs.
    // The docs are here:
    // https://www.npmjs.com/package/cors#configuration-options
    methods: ["GET", "POST"],
    allowedHeaders: ["content-type"]
}

EDIT 1: Now that I know you are using Socket.IO 3.0, I may be able to help you better.

Socket.IO 3.0 includes a bunch of breaking changes (see the full changelog here), so that may be why you are getting that TypeError: io.origins is not a function.

As for that CORS error, Socket.IO 3.0 removed some old options, and added some new ones for CORS management. One of the removed options was the origins property. That's why your code is not working: you aren't actually setting any CORS configurations. To set CORS configurations in Socket.IO 3.0, use the options below:

// I still use a port here, because it works, but you could use @Aryas'
// code if you wish.
const io = socketIO(3000, {
  // Now, the CORS config.
  // You could either use the new `cors` property...
  cors: {
    origin: "https://example.com",
    methods: ["GET", "POST"],
    allowedHeaders: ["content-type"]
  },
  // ...Or the old `allowRequest` function.
  allowRequest: function(req, callback) {
    callback(null, req.headers.referer.startsWith("https://example.com"));
  }
});

Oh, and, setting CORS options in Express is not necessary; Socket.IO picks up requests that are for it automatically, before any Express handlers are called.

ORIGINAL ANSWER:
It's telling you that because your io variable is not an instance of the Socket.IO server. Instead, it's the Socket.IO server constructor. Why?

Well, you have this code:

// You are passing two parameters to `require`: "socket.io", and 3000
var io = require("socket.io", 3000);

require returns whatever is exported by the module, which, in this case, is the Socket.IO server constructor (not an instance of it). The 3000 you passed to require doesn't do anything, since require only accepts one argument.

In order to get a Socket.IO server instance, you need to do this:

// The clearer way:
// `socketIO` is the Socket.IO server constructor.
var socketIO = require("socket.io");
// `io` is the Socket.IO server instance.
var io = socketIO(3000);

// The shorter way:
// Calling the result of `require` (the Socket.IO server constructor) as a function.
var io = require("socket.io")(3000);
Take-Some-Bytes
  • 918
  • 1
  • 8
  • 21
-1

UPDATE 2

As you mentioned in this https://pastebin.com/1TDhWxaC code I found that you are using fire fox try this code on Chrome or other browser's i think it will work on that browser

There is bug or problem certificate or something else in in firefox you will notice this in all below cases

I suggest you to go through below questions and answers

  1. Firefox 'Cross-Origin Request Blocked' despite headers
  2. CORS request did not succeed on Firefox but works on Chrome
  3. Socket.io + Node.js Cross-Origin Request Blocked
  4. CORS Blocked with node.js and socket.io
  5. Socket.IO: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

//Error in Firefox client: //Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://<MY_EXTERNAL_IP>:3000/socket.io/?EIO=3&//transport=polling&t=NMX8aWc. (Reason: CORS request did not succeed)

UPDATE 1 IN your app.js or index.js put below code

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", '*');
  res.header("Access-Control-Allow-Credentials", true);
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header("Access-Control-Allow-Headers", 'Origin,X-Requested-With,Content-Type,Accept,content-type,application/json');
  res.header("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0");
  next();
});

If you are using firefox refer to this answer : Answer on stackOverflow

And Install cors If you haven't used in your app

You can use cors npm in express.

npm install cors

Update app.js

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())

ORIGINAL ANSWER Try This maybe it could Work for you it is working fine in my app

var express = require('express');
var app = express();
var http = require('http').Server(app);
const io = require('socket.io')(http, {
  origins: '*:*'
});

http.listen(port);
console.log('Server is running on port:' + port);
Aryan
  • 3,338
  • 4
  • 18
  • 43
  • Well I'm not getting that error anymore but I am getting the Cross-Origin Request Blocked Error on the client :( – Ran Shorowitz Nov 07 '20 at 04:06
  • Added to main post – Ran Shorowitz Nov 07 '20 at 04:15
  • Looks like I'm using 3.0.0 – Ran Shorowitz Nov 07 '20 at 04:19
  • i know but i said if your using firefox as mentioned in the whole question – Aryan Nov 07 '20 at 05:24
  • Maybe 3.0.0 is just bugged. I'm getting this error on Chrome: Failed to load resource: net::ERR_CONNECTION_TIMED_OUT Same thing if I downgrade to 2.3.0 Also all those answers indicate you need to visit the IP address of the server and manually allow the certificate, but when I try to go to the IP address (and port) of the socket.io server in the browser, it never loads the page. Nothing happens. – Ran Shorowitz Nov 07 '20 at 06:01
  • @RanShorowitz this my code of app.js and it is working fine. https://pastebin.com/V4iSkDYc i am using socket.io@2.3.0 – Aryan Nov 07 '20 at 06:13
  • But where is your socket.io javascript client code? All I see is your server code. This is my server and my client code: https://pastebin.com/E10B2qLH As well as the errors I'm getting below on Firefox and Chrome. I'm using 2.3.0 on both now but still getting that error. I'm honestly at a loss at this point. I've tried so many different solutions, none are working. This CORS error will not go away. – Ran Shorowitz Nov 07 '20 at 06:51
  • https://pastebin.com/RfqBPZpQ here is both server and client side code – Aryan Nov 07 '20 at 07:03
  • I think yours works because you're connecting through localhost. I'm connecting to an external IP address, hence the Cross Origin Domain error – Ran Shorowitz Nov 07 '20 at 07:06
  • I don't think the code is the problem honestly. I tested a local IP address on my network and it worked. But external IP address does not connect at all. Just gives those two errors. Just can't figure out why external IP won't work. My latest code: https://pastebin.com/0YRzx3sz – Ran Shorowitz Nov 08 '20 at 05:00