13

Generalizing that would be the question... how to make websockets to go through a proxy in node.js?

In my particular case I'm using pusher.com with the node.js client library they recommend. Looking inside the code I would like to know some hints on what I should change in order to make this library to work with a proxy... you can take a look in the code here

Maybe I should somehow replace or modified the websockets module that is being used by the library?

EDIT

Thanks for your answers/comments! A couple of things to take into consideration (excuse me if I'm wrong with some/all of them, just learning):

  • I don't want to create a proxy server. I just want to use an existent proxy server within my company in order to proxified my websockets requests (particularly pusher.com)
  • Just to let you know, if I use a proxifier like the one for windows Proxifier and I set up the rule to inspect for all connections to port 443 to go through the proxy server proxy-my.coporate.com:1080 (type SOCKS5) it works like a charm.
  • But I don't want to go this way. I want to programatically configuring this proxy server within my node js code (even if that involved to modified the pusher library I mentioned)
  • I know how to do this for HTTP using Request module (look for the section that mentions how to use a proxy).
    • I want a similarly thing for websockets.
8
  • you can use pusher's http features to interact in nearly the same way as sockets, if nothing else...
    – dandavis
    Commented May 22, 2014 at 2:52
  • You are right about inspecting the websockets module... it should the CONNECT method on a configured proxy. Where does that module live? Commented May 22, 2014 at 9:09
  • Thanks for your comments! I've expanded my question. Commented May 22, 2014 at 14:23
  • If it helps you can check socket.io-proxy, which is a library build on top of websockets.
    – Razvan
    Commented May 23, 2014 at 15:03
  • @dandavis in pusher.com I only found HTTP REST API for server side... for client side (which is what I want, to wait for events in a channel) I didn't find anything. Commented May 23, 2014 at 20:53

4 Answers 4

12

From https://www.npmjs.com/package/https-proxy-agent

var url = require('url');
var WebSocket = require('ws');
var HttpsProxyAgent = require('https-proxy-agent');

// HTTP/HTTPS proxy to connect to
var proxy = process.env.http_proxy || 'http://168.63.76.32:3128';
console.log('using proxy server %j', proxy);

// WebSocket endpoint for the proxy to connect to
var endpoint = process.argv[2] || 'ws://echo.websocket.org';
var parsed = url.parse(endpoint);
console.log('attempting to connect to WebSocket %j', endpoint);

// create an instance of the `HttpsProxyAgent` class with the proxy server information
var options = url.parse(proxy);

var agent = new HttpsProxyAgent(options);

// finally, initiate the WebSocket connection
var socket = new WebSocket(endpoint, { agent: agent });

socket.on('open', function () {
  console.log('"open" event!');
  socket.send('hello world');
});

socket.on('message', function (data, flags) {
  console.log('"message" event! %j %j', data, flags);
  socket.close();
});
1
  • It works using this method,thanks.
    – inix
    Commented Nov 18, 2021 at 23:04
3

Using a proxy for websockets should work roughly the same as for https connections; you should use the CONNECT method. At least that's what both the HTTP and HTML5 specs say. So if your proxy implements CONNECT, you're good to go.

3
  • @Chris Wesseling I know this is more than a year later... I have implemented a node proxy using http and net modules and it works fine with both http and https. However, my socket.io programs fail. How do I get socket.io to use wss as opposed to ws? Thats for my reverse proxy. Now for the forward proxy, how do I use sites using socket.io if they are not using wss and my forward proxy has no express support for websockets?
    – Sunny
    Commented Dec 20, 2015 at 19:20
  • @samir Your comment looks like 2 questions. Try asking them and reference this question or my answer as related research you've done. Starting a new thread in the comments is considered bad form. Commented Dec 21, 2015 at 6:55
  • @Chris Wesseling Fair enough. I have posted a new question here: [stackoverflow.com/questions/34394338/…
    – Sunny
    Commented Dec 21, 2015 at 11:14
2
+25

Try node-http-proxy

It allows you to send http or websocket requests through a proxy.

var http = require('http'),  
httpProxy = require('http-proxy');
//
// Create a basic proxy server in one line of code...
//
// This listens on port 8000 for incoming HTTP requests 
// and proxies them to port 9000
httpProxy.createServer(9000, 'localhost').listen(8000);

//
// ...and a simple http server to show us our request back.
//
http.createServer(function (req, res) {  
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

Source: link

4
  • Thanks for sharing! I'll try this out... I was reading a little bit and stumbled upon this other stackoverflow post which also reference the library you're saying. If you can expand with a concrete example or using node-http-proxy with websockets applicable for pusher.com it will be appreciated. Commented May 19, 2014 at 14:12
  • 3
    afaict this implements a proxy and doesn't explain how opening a socket through a proxy works. Can you point me to something I'm missing? Commented May 22, 2014 at 8:53
  • Well, you can simply use the websocket protocol with the proxy (wss://) and you're proxying websockets! Commented May 23, 2014 at 15:19
  • 4
    I don't think this answers the question... This simply adds an extra middle man. It doesn't show how to connect to a remote ws endpoint through a proxy. OP - did you work out a solution to this? Commented Apr 11, 2017 at 15:07
2

Most web proxies don't support websockets yet. The best workaround is to use encryption by specifying wss:// (websocket secure protocol):

wss://ws.pusherapp.com:[port]/app/[key]
4
  • Thanks for your answer Rudolf! I'm quite confuse... in my question I've posted a link to the code that interacts with the pusher api which uses the encryption wss:// as your're saying, Am I missing something in order to make it work behind a proxy server? Commented May 19, 2014 at 13:32
  • The other answers here seems to assume you're trying to setup your own node.js proxy for your websockets. Is your problem that your end-users are behind proxy servers and their browsers are therefore unable to establish a connection? Do you have any control over the proxy or know which version it is? Commented May 20, 2014 at 14:38
  • 1
    Correct. My problem is that I have to use a corporate proxy in order to be able to connect to the pusher web socket endpoint. Commented May 20, 2014 at 23:02
  • Using ssl works because it will use the CONNECT method. If in your code you do the same, you get the same result. I expect wss: will trigger the same timeouts that @dandavis mentions. Commented May 22, 2014 at 8:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.