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

add support for proxying websockets #126

Open
5im-0n opened this issue Jul 12, 2019 · 5 comments
Open

add support for proxying websockets #126

5im-0n opened this issue Jul 12, 2019 · 5 comments

Comments

@5im-0n
Copy link

5im-0n commented Jul 12, 2019

When proxying a websocket with --rewrite '/ws/(.*) --> https://someserver.com/ws/$1' local-web-server somehow mangles the response.

@75lb
Copy link
Member

75lb commented Jul 15, 2019

lws-rewrite does not currently support rewriting Websocket requests (which can only be proxied via HTTP Connect tunnelling) but i've starting looking for a solution which also honours corporate HTTP proxies.

@75lb 75lb changed the title proxying websockets somehow does not work add support for proxying websockets Jul 15, 2019
@tiagostutz
Copy link

Hi! Still needing some help with this?
If so, do you have any contributing guidelines or something like this?

We are adopting Open Source Friday here at Banco do Brasil and I can take this one on my Open Source Fridays.

@75lb
Copy link
Member

75lb commented May 8, 2020

Yes, help welcome thanks.. I should write some contributing guidelines - will add it to my list, thanks.

@donald-stolz
Copy link

Any update on this issue? I recently ran into it as well

@svercammen
Copy link

I solved the issue of forwarding a Next.JS HRM websocket connection by writing some middleware in file proxy/mw-websocket.mjs:

import { WebSocket, WebSocketServer } from 'ws';

const HMR_PATH = '_next/webpack-hmr';

class Websocket {
  middleware(config, lws) {
    const wss = new WebSocketServer({ server: lws.server, path: `/${HMR_PATH}` });

    wss.on('connection', (serverSocket) => {
      const target = `${config.wsRewrite}/${HMR_PATH}`;
      console.log(`WebSocket connection opened; forwarding to ${target}`);

      const clientSocket = new WebSocket(target);
      clientSocket.on('message', (message) => {
        // console.log(`Received from upstream: ${message}`);
        serverSocket.send(message.toString());
      });

      serverSocket.on('message', (message) => {
        // console.log(`Received from browser: ${message}`);
        clientSocket.send(message.toString());
      });
    });
  }
}

export default Websocket;

Configure it as follows. Target address in in property wsRewrite and don't forget to include the middleware in the stack. The stack following the middleware is the default stack, feel free to adjust to your needs:

module.exports = {
  [... your other configuration],
  wsRewrite: 'ws://localhost:4200',
  stack: [
    'proxy/mw-websocket.mjs',
    'lws-body-parser',
    'lws-request-monitor',
    'lws-log',
    'lws-cors',
    'lws-json',
    'lws-rewrite',
    'lws-blacklist',
    'lws-conditional-get',
    'lws-mime',
    'lws-compress',
    'lws-spa',
    'lws-static',
    'lws-index',
  ],
};

Note that this only forwards connections to one specific path and performs absolutely zero error checks, which is fine for my application. Ideally this use case would at some point be supported but for now this is a functioning workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants