0

I am working on a chrome extension that uses socket IO to communicate with a backend server. The problem is that my connection seems to be blocked by cross origin policies.

My server is running on localhost, bit the chrome extension that i am loading from local directory has some other "origin" header and thus my requests are blocked.

How can I allow a socket IO connection from local chrome extension ?

The request:

https://localhost:8000/socket.io/?EIO=3&transport=polling&t=LkNvOuH net::ERR_CONNECTION_CLOSED

The headers:

Provisional headers are shown
Accept:*/*
Origin:chrome-extension://acgnndapfmilgphkhkdkipfiipikkjki
Referer:https://docs.google.com/presentation
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36
sashok_bg
  • 2,436
  • 1
  • 22
  • 33

2 Answers2

1

You may want to check this documentation which shows how you can properly integrate Socket.IO.

As detailed, Socket.IO is composed of two parts:

  • A server that integrates with (or mounts on) the Node.JS HTTP Server: socket.io
  • A client library that loads on the browser side: socket.io-client

Note also that during development, socket.io serves the client automatically, so you only have to install one module:

npm install --save socket.io

That will install the module and add the dependency to package.json. Edit index.js to add it:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Furthermore,

Notice that I’m not specifying any URL when I call io(), since it defaults to trying to connect to the host that serves the page.

Lastly, you may want to also check the suggested solutions from this thread or from this related SO post.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
0

In Case you use Express framework on backend, I solved it by adding cors middleware to the server and i added this line app.use(cors())

Styxali
  • 66
  • 4