Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Socket.IO connection blocked by CORS between your page and your Node.js server

ErrorCross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=4&transport=polling. (Reason: CORS header 'Access-Control-Allow-Origin' missing)

Short answer

Since Socket.IO v3 the server sends no CORS headers until you set the cors option on the Server itself. Express middleware does not do it for you. List the exact page origin there, match credentials on the client, and check that the client really reaches the Socket.IO path, because a wrong host also shows as a CORS error.

  • Applies to Socket.IO 4, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Ask the server for the handshake the way the browser would, with the page's origin. A healthy server answers with a body that starts with 0 and carries an Access-Control-Allow-Origin line.

    curl -i "http://localhost:3000/socket.io/?EIO=4&transport=polling" \
      -H "Origin: http://localhost:5173"
  2. 2Match what you see, then jump to its fix

    1. 1The curl answer has a body but no Access-Control-Allow-Origin headerThe Server was created without a cors optionFix
    2. 2The client sets withCredentials: true and the server answers with origin: '*'Cookies or credentials are used together with a wildcard originFix
    3. 3The curl in the first step returns an HTML page, a 404 or no answer, not a body starting with 0The client never reaches a Socket.IO server at allFix

What does the error look like?

  • Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/socket.io/?EIO=4&transport=polling. (Reason: CORS header 'Access-Control-Allow-Origin' missing)
  • Access to XMLHttpRequest at 'http://localhost:3000/socket.io/?EIO=4&transport=polling' from origin 'http://localhost:5173' has been blocked by CORS policy

Cause 1 of 3

The Server was created without a cors option

1Check

Socket.IO v3 and later require CORS to be enabled explicitly on the Socket.IO server. Page and server on different ports or hosts are different origins, so the polling request is blocked. Note that WebSocket frames are not subject to CORS; the first HTTP long-polling request is what fails.

grep -n "new Server" -r src server.js 2>/dev/null

2Fix

Pass the origin of the page in the cors option. The option is called origin, singular, and takes an array for several sites.

import { createServer } from 'node:http';
import { Server } from 'socket.io';

const httpServer = createServer(app);
const io = new Server(httpServer, {
  cors: {
    origin: ['http://localhost:5173', 'https://app.example.com'],
  },
});
httpServer.listen(3000);

3Verify

Repeat the curl; the header must now be present and equal to the origin you sent.

curl -si "http://localhost:3000/socket.io/?EIO=4&transport=polling" \
  -H "Origin: http://localhost:5173" | grep -i access-control-allow-origin

Confirmed by socket.io

Did this fix it?

Cause 2 of 3

Cookies or credentials are used together with a wildcard origin

1Check

The server options page states that origin '*' cannot be combined with credentials: true, because browsers block that pair. Look at both sides of the connection.

grep -n "withCredentials" -r src
grep -n "credentials" -r server.js src

2Fix

Name the exact origin on the server, turn credentials on there, and keep withCredentials on the client.

// server
const io = new Server(httpServer, {
  cors: { origin: ['https://app.example.com'], credentials: true },
});

// client
import { io } from 'socket.io-client';
const socket = io('https://api.example.com', { withCredentials: true });

3Verify

The handshake response now carries Access-Control-Allow-Credentials: true.

curl -si "https://api.example.com/socket.io/?EIO=4&transport=polling" \
  -H "Origin: https://app.example.com" | grep -i access-control

Confirmed by socket.io

Did this fix it?

Cause 3 of 3

The client never reaches a Socket.IO server at all

1Check

The troubleshooting guide says the same browser message appears when the client is not reaching the Socket.IO server at all. Client and server must also agree on the path option; the default is /socket.io/, and a namespace in the URL does not change it.

curl -i "http://localhost:3000/socket.io/?EIO=4&transport=polling"
grep -n "path" -r server.js src

2Fix

Point the client at the host and port where the server listens, and give both sides the same path if you changed it.

const io = new Server(httpServer, { path: '/realtime/', cors: { origin: ['https://app.example.com'] } });
const socket = io('https://api.example.com', { path: '/realtime/' });

3Verify

The handshake URL with your path returns a body starting with 0.

curl -s "https://api.example.com/realtime/?EIO=4&transport=polling"

Confirmed by socket.io

Did this fix it?

How do I stop it coming back?

  • Keep the allowed origins in one environment variable and use it for both the HTTP API and Socket.IO, so they cannot drift.
  • Never ship origin: '*' on a server that also uses cookies.
  • Add the curl handshake to your deploy checks, so a missing header shows up before users do.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

The browser blocks your API response: no Access-Control-Allow-Origin header

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.