Node proxy web sockets how to check
Asked Answered
L

2

10

I use the following module and it works fine for reverse proxy https://github.com/nodejitsu/node-http-proxy currently I've used the code like the following example

httpProxy.createServer({
  target: 'ws://localhost:9014',
  ws: true
}).listen(8014);

my question is how can I check/simulate that the websockets are working? Any test will be helpful...

Laurentia answered 5/9, 2015 at 17:55 Comment(0)
Z
6

In response to the OP's request for browser test, I modified my original solution to proxy both HTTP and WS traffic to a server where an index.html file is served. This file then connects the browser to the proxy server via WebSocket, which the proxy then proxies to the main server. A simple message is printed on the browser document from the main server.

So that there is no need to copy/paste anything, I created this repo with full instruction: https://github.com/caasjj/httpproxy.git

Here is the code in case others want to look at it here. To run the whole thing, create the two server files and the index.html file, start the servers with node proxyreceiver.js and node proxyserver.js and then navigate to localhost:8014/index.html.

(proxyserver.js):

var httpProxy = require('http-proxy');
var http = require('http');

var proxy = new httpProxy.createProxyServer({
  target: {
    host: 'localhost',
    port: 9014
  }
});
var proxyServer = http.createServer(function (req, res) {
  proxy.web(req, res);
});

//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {
  proxy.ws(req, socket, head);
});

proxyServer.listen(8014);

(proxyreceiver.js):

var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');

app.listen(9014);

function handler (req, res) {
    res.writeHead(200);
    fs.readFile('index.html', function(err, data){
      res.end(data);
    })

}

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

  socket.emit('data', { message: 'Hello World!' });

  socket.on('resp', function(msg) {
    console.log('Got message: ', msg);
  });

});

(index.html):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Web Socket Proxy Test</title>
  <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
  <script>
    var socket = io('http://localhost:8014');
    var p = document.createElement("p")
    socket.on('data', function (data) {
       console.log('Got', data);
       p.innerHTML = "Received:" + data.message;
       document.body.appendChild(p);
    });
</script>
</head>
<body>
  <h1>Test ProxyServer</h1>
</body>
</html>
Zubkoff answered 10/9, 2015 at 18:25 Comment(6)
Thank you very much 1+,I created those 3 files and start the node app and didnt get any error, the issue is that when I put in the browser the url(like in the post 8014/9014) nothing happen even in the console,any idea what is missing?Do I need additional step (im new to this topic :))Laurentia
Please note that we are not returning any renderable html to the browser. Your original question just needed test code to verify the connection, and that's how I wrote the answer. Were you able to see the client connect to the server, have the request proxied and then get a response? If so, that verifies that the web socket connection is working through the proxy server. If you need browser test code, I will need to provide you additional code.Zubkoff
Updated with test code to run on browser. Please let me know if this is closer to what you needed.Zubkoff
Thanks a lot,I clone it from git and start the two files, when I run the index file I got the test ProxyServer but how I acatully test the webSocket when I put in the url localhost:8014/index.html noting happen in console I see Failed to load resource: localhost:8014/index.html net::ERR_CONNECTION_REFUSED any idea?Laurentia
Second question is assume that I dont need the second server in my scenario ( this server is created by other program and I need just to proxy to it calls ans ws how would I adopt the code than,Thanks in advance expert!Laurentia
The error is because the proxy can't connect to the main WS server. Please make sure you have the ports exactly as I have them, both servers are on the same machine, no firewalls, etc. For your 2nd question, it depends on the configuration (port #) of your main server, but you shouldn't need to change much.Zubkoff
A
1

The best way to test is to create a client to connect to it. there are many ws modules around. Or you can use this: https://www.websocket.org/echo.html just put your url there and test it.

Anaanabaena answered 9/9, 2015 at 15:50 Comment(2)
Can you please provide example how can I create client to connect to it?Laurentia
just use the one i provided in the link. If you want to create a client you can use node.js and use a module called wsAnaanabaena

© 2022 - 2024 — McMap. All rights reserved.