Trying to make WebSocket proxy

Hello, im trying to make WebSocket proxy but it fails everytime i connect (with no error or reason in client)

Server code is:

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


var app = express();
var proxy = httpProxy.createProxyServer({ ws: true, secure: false });

var server = http.createServer(app);
server.on('upgrade', function (req, socket) {
  proxy.ws(req, socket, {
    target: "wss://echo.websocket.org",
  });
});

const listener = server.listen(process.env.PORT, () => {
  console.log("Listening on " + listener.address().port);
});

When i do:

var ws = new WebSocket("wss://ubiquitous-pushy-singularity.glitch.me/");

in client, it says: WebSocket connection to 'wss://ubiquitous-pushy-singularity.glitch.me/' failed:

Can anyone help me please?

1 Like

Is there more to the error.

nope, just that it failed no more errors in client or server

Does the server receive the requests

yes, i tried console logging in the upgrade and it worked

I would ask if you are following some working example somewhere. I just did a quick search and no JS example I saw monitored for an ‘upgrade’ message. Not saying it isn’t there or can’t be used just that maybe it is handled differently with NodeJs or that particular library. I’d be shocked to learn there wasn’t an example on the Internet using that exact library.

I’d try copying something from an article that demonstrated it connected and that hello world worked.

I don’t know what the problem is, but thought I’d drop in to say that I believe the project in it’s current state is drawing wisdom from this stack overflow answer: node.js - Using node http-proxy to proxy websocket connections - Stack Overflow

yes thats where i got it from

I don’t know the answer either but I note that example is over 7 years old. There may be more effective ways of doing it since then… I don’t use Express but I have to believe you could find a working example of a project rather than a message.

I will probably put something together but it won’t be likely be this week and I will no doubt use the Hapi server which I like a lot. It is nice to use libraries that still have support and active development.

Again I’m not recommending one thing over another just suggesting that you may need to look around for slightly newer or more comprehensive examples.

oh this article is from 2020 Websocket Server in Node.js - Mastering JS but it uses a library named ws. You might consider giving it a try as they demonstrate it working with Express.

Depending on your requirements this may not be enough, but a very basic WebSocket proxy can be written like this:

const express = require('express')
const http = require('http')
const WebSocket = require('ws')

const app = express()

const server = http.createServer(app)
const wss = new WebSocket.Server({server})

const checkAlive = setInterval(() => {
  wss.clients.forEach(ws => {
    if (ws.isAlive === false) {
      return ws.terminate()
    }

    ws.isAlive = false
    ws.ping()
  })
}, 60000)

const proxyMessage = (target, message) => {
  if (target.readyState === WebSocket.OPEN) {
    return target.send(message)
  }
  
  target.on('open', () => {
    target.send(message)
  })
}

wss.on('connection', ws => {
  const target = new WebSocket('wss://echo.websocket.org')
  
  target.on('close', () => {
    ws.terminate()
  })
  
  target.on('message', message => {
    ws.send(message)
  })
  
  ws.isAlive = true
  ws.on('pong', () => {
    ws.isAlive = true
  })
  
  ws.on('close', () => {
    target.terminate()
  })
  
  ws.on('message', message => {
    proxyMessage(target, message)
  })
})

wss.on('close', () => {
  clearInterval(checkAlive)
})

server.listen(process.env.PORT)