Santi Agüero
Santi Agüero

Reputation: 3133

How to make websockets to go through a proxy in node.js

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):

Upvotes: 13

Views: 43320

Answers (4)

YourBestBet
YourBestBet

Reputation: 1749

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();
});

Upvotes: 12

Chris Wesseling
Chris Wesseling

Reputation: 6388

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.

Upvotes: 3

Rudolf Meijering
Rudolf Meijering

Reputation: 1587

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]

Upvotes: 2

ThePixelPony
ThePixelPony

Reputation: 741

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

Upvotes: 2

Related Questions