socket.io plus node-http-proxy

1,164 views
Skip to first unread message

DaveM

unread,
Mar 1, 2011, 3:14:53 PM3/1/11
to Socket.IO
Anyone know how to set up node.js to do both socket.io and reverse
proxy.

That is:
all requests on port 80 / to be reverse proxied to port 8080
except /socketio, which subscribes to redis.

I can get both working independently (socketio and node-http-proxy)
but not together.

This is to replace nginx which was reverse proxying, but aparently
can't proxy websockets.

thanks

Marak Squires

unread,
Mar 1, 2011, 4:25:44 PM3/1/11
to sock...@googlegroups.com, DaveM, fedor....@gmail.com
It looks like there is an upgrade event in http-proxy which should then utilize proxyWebSocketRequest.


I'm not sure if this is working? I think Fedor was working on this last. 

- Marak

DaveM

unread,
Mar 1, 2011, 9:42:19 PM3/1/11
to Socket.IO

DaveM

unread,
Mar 1, 2011, 9:47:53 PM3/1/11
to Socket.IO
This script, works, sort of, but randomly crashes node:
Uses several web servers, one to accept all requests, send static
files, and two more for proxied requests (/handler -> 8080, rest go to
socket.io) - curiously this seems to be the only combination that
works for me.

---------------------------------------------

var express = require('express');
const redis = require('redis');
const client = redis.createClient();
const io = require('socket.io');
var http = require ('http')
var httpProxy = require ('http-proxy')

http.createServer (function (req, res) {

var proxy = new httpProxy.HttpProxy (req, res)

console.log (req.url)
if (req.url.indexOf ('/handler') > -1)
{
proxy.proxyRequest (8080, 'localhost', req, res)
}
else
{
proxy.proxyRequest (3000, 'localhost', req, res)
}
}).listen (7000)


var app = module.exports = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));

if (!module.parent) {
app.listen (3000, 'localhost');
console.log ("Express server listening on port %d", app.address
().port)

const socket = io.listen (app);

socket.on ('connection', function (client) {
const subscribe = redis.createClient ();
subscribe.subscribe('pubsub');

subscribe.on ("message", function (channel, message) {
client.send(message);
});

client.on('message', function (msg) {
console.log (msg)
});

client.on('disconnect', function () {
subscribe.quit ();
});
});
}








On Mar 1, 12:14 pm, DaveM <dmos...@gmail.com> wrote:

DaveM

unread,
Mar 2, 2011, 2:09:44 AM3/2/11
to Socket.IO
Managed to get this working, but with Faye, not Socket.io !
listens on port 8000, proxies non comet traffic to 8080, and handles
comet much faster than socket.io, without the crashes).

---------------

var http = require ('http'),
faye = require ('faye'),
httpProxy = require ('http-proxy')

var server = http.createServer (function (req, res) {

new httpProxy
.HttpProxy (req, res)
.proxyRequest (8080, 'localhost', req, res)
})

new faye.NodeAdapter ({
mount: '/faye',
timeout: 45
}).attach (server);

server.listen (8000);

--------------
Reply all
Reply to author
Forward
0 new messages