Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix socket.io endpoint path #10858

Merged
merged 1 commit into from
Mar 10, 2022
Merged

Conversation

paul-marechal
Copy link
Member

Socket.io will try to connect to domain.com/socket.io to establish
communication by default. This is problematic when the frontend is
served on an HTTP path that isn't /.

Update the path supplied to Socket.io to take the current location into
account.

How to test

  • Build and run this PR on localhost:3000.
  • Run the proxy script below like node index.js 4000 /random-string/ http://localhost:3000/
  • Connect to localhost:4000/random-string/ with your browser. It should work.

package.json:

{
  "name": "http-proxy-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "http-proxy": "^1.18.1"
  },
  "devDependencies": {
    "@types/http-proxy": "^1.17.8"
  }
}

index.js:

const http = require('http')
const httpProxy = require('http-proxy')

let [_node, _script, port, proxyPathPrefix, target] = process.argv

const proxy = httpProxy.createProxyServer({ ws: true, target })
http.createServer((req, res) => {
    if (req.url.startsWith(proxyPathPrefix)) {
        req.url = req.url.substring(proxyPathPrefix.length - 1)
        proxy.web(req, res, undefined, error => console.error(error))
    } else {
        res.statusCode = 404
        res.write(`404: ${req.url}`)
        res.end()
    }
}).on('upgrade', (req, socket, head) => {
    if (req.url.startsWith(proxyPathPrefix)) {
        req.url = req.url.substring(proxyPathPrefix.length - 1)
        proxy.ws(req, socket, head, undefined, error => console.error(error))
    } else {
        socket.end()
    }
}).listen(port)

Review checklist

Reminder for reviewers

@paul-marechal paul-marechal added core issues related to the core of the application websocket issues related to websockets/messaging labels Mar 8, 2022
msujew
msujew previously approved these changes Mar 8, 2022
Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking care of this Paul, looking good!

@msujew msujew dismissed their stale review March 8, 2022 12:36

Nevermind, I just noticed that it breaks on Electron. We should probably only run it when !isElectron?

@paul-marechal
Copy link
Member Author

@msujew good catch, it should work on Electron now!

Socket.io will try to connect to `domain.com/socket.io` to establish
communication by default. This is problematic when the frontend is
served on an HTTP path that isn't `/`.

Update the path supplied to Socket.io to take the current location into
account.
Copy link
Member

@msujew msujew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on both platforms now, thanks 👍

@paul-marechal paul-marechal merged commit ac48d7e into master Mar 10, 2022
@paul-marechal paul-marechal deleted the mp/socket.io-path-fix branch March 10, 2022 20:01
@github-actions github-actions bot added this to the 1.24.0 milestone Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core issues related to the core of the application websocket issues related to websockets/messaging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants