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

node-http-proxy and websockets #577

Open
rere16 opened this issue Feb 7, 2014 · 10 comments
Open

node-http-proxy and websockets #577

rere16 opened this issue Feb 7, 2014 · 10 comments

Comments

@rere16
Copy link

rere16 commented Feb 7, 2014

Hello,

I am using node-http-proxy to route websocket from port 80 to port 8080. I have a http server (port 80). I would like to create a websocket connection between the client and the server through the nodejs server. I use the following source code :

var proxy = new httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 8080
  }
});
var proxyServer = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
});

proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

proxyServer.listen(80);

But, the server only receives the handshake message and the connection closes (eof). I tested with apache and proxy_mod, the server works fine.

Any idea ?

Thank you very much.

@jcrugzz
Copy link
Contributor

jcrugzz commented Feb 7, 2014

@rere16 you also have to proxy the web requests in the proxyServer as websockets is initialized by a couple POST requests iirc

@rere16
Copy link
Author

rere16 commented Feb 8, 2014

Thank you very much for your answer. But, I have no idea how to do this. Please, could you give me a short example ?

Thanks

@jcrugzz
Copy link
Contributor

jcrugzz commented Feb 9, 2014

@rere16 well lets start with why do you want to proxy the websocket connection to another server when you already have an http server that you can then attach the websocket server to as well?

@rere16
Copy link
Author

rere16 commented Feb 9, 2014

Because I developed an application using websocket (port 8080) and I would like that this application communicates with the client through the webserver (port 80). By proxing, only the port 80 is open. The port 8080 is local. (The webserver and the application is on the same machine)

@jcrugzz
Copy link
Contributor

jcrugzz commented Feb 9, 2014

@rere16 ahh ok, I guess my argument is why are they two separate applications? The tricky part here is that you will have to custom proxy based on the particular requests that initialize the websocket connection.

@rere16
Copy link
Author

rere16 commented Feb 12, 2014

There are two applications because one is an application written in C++ using websocket and the other is the webserver (nodejs). I just want to redirect the websocket stream between the client and the application (no cache). It's a routing proxy feature.

For example with apache (port 80)

Location /ws
ProxyPass ws://127.0.0.1:8080
ProxyPassReverse ws://127.0.0.1:8080
Location

The request ws://127.0.0.1:80/ws will be redirect to ws://127.0.0.1:8080/ws.

Thx

@gara-MI
Copy link

gara-MI commented Jul 29, 2014

@rere16 I am also in this situation. I have websocket server written in c++ and generating some data and I want to use node-http-proxy.
I am wondering whether you are able to find the solution for this or not.

Please let me know if you did this already
Thanks

@gara-MI
Copy link

gara-MI commented Jul 29, 2014

Did some one find the solution for this problem?

@glortho
Copy link

glortho commented Jan 30, 2015

+1

@rere16
Copy link
Author

rere16 commented Feb 11, 2015

The javascript source code in my first message works. I had an issue in my C++ program.

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

No branches or pull requests

4 participants