redbird icon indicating copy to clipboard operation
redbird copied to clipboard

usage with socket.io

Open Kaliph opened this issue 5 years ago • 1 comments

I am trying to use redbird as a reverse proxy to horizontally scale my node.js application.

My redbird configuration looks like this so far.

`const proxy = require('redbird')({ port: env.PROXY_HTTP_PORT, // Specify filenames to default SSL certificates (in case SNI is not supported by the // user's browser) xfwd: false, ssl: { port: env.PROXY_HTTPS_PORT, key: "ssl/privkey.pem", cert: "ssl/fullchain.pem", } });

proxy.register("localhost", "http://127.0.0.1:8081", {ssl: true}); proxy.register("localhost", "http://127.0.0.1:8082", {ssl: true});`

Then my express and socketio code looks like this.

`// HTTP var serv = http.createServer(app) .listen(env.WEBSERVER_PORT_MAIN, function () { logger.info('Server listening on port ' + env.WEBSERVER_PORT_MAIN); });

// HTTPS

var server = https.createServer({ key: fs.readFileSync('ssl/privkey.pem'), cert: fs.readFileSync('ssl/fullchain.pem'), ca: fs.readFileSync('ssl/fullchain.pem'), requestCert: env.WEBSERVER_REQUEST_CERT, rejectUnauthorized: env.WEBSERVER_REJECT_UNAUTHORIZED }, app).listen(env.WEBSERVER_SECUREPORT_MAIN, function () { logger.info('Secure Server listening on port ' + env.WEBSERVER_SECUREPORT_MAIN); });

// launch Socket.io

var io = require('socket.io')(server);

io.on('connection', async function (socket) {

}`

My mainserver is listening on port 8081 for http traffic and 8444 is for https traffic. All http traffic gets redirected to https from my server. The same applies for my backup server which is listening on port 8082 for http and 8445 for https traffic. Socket.io is using my https server to start connections.

If I know get to https://localhost redbird redirects me to one of my servers redirecting my to

https://localhost:8444/login/index.html

for example.....

This works fine so far. When I now login into my webpage, the webpage starts using socket.io then I get the following error in the browser console. It seems redbird is not yet handling my socket connection

Access to XMLHttpRequest at 'https://localhost/socket.io/?EIO=3&transport=polling&t=MzmUmft' from origin 'https://localhost:8444' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any help on how to use redbird with socket.io would be helpful. Thankyou

Kaliph avatar Jan 04 '20 12:01 Kaliph

This is because the local host is not the same as the host of the server.

Just make your server set the header to fix this issue.

We have the same issue in development because angular app localhost:4200 is not the same as the server in docker localhost:3000

vorticalbox avatar Jan 28 '20 18:01 vorticalbox