0
votes

I'm using Socket.io ver 1.0.6. I have problem with using socket.io and apache proxy.

Proxy have defined in vhots and look like this:

    ServerAdmin webmaster@localhost
    ServerName site.loc
    DocumentRoot /home/site/Private/dev/site

    ProxyPass /node http://localhost:5000
    ProxyPassReverse /node http://localhost:5000

so url http://site.loc/node should run http://site.loc:5000

node server look like this:

var port = 5000;
var server = require('http').Server();
var io = require('socket.io')(server);

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

});

server.listen(port);
console.log('Listening on port: ' + port);

and client:

var socket = io.connect('ws://site.loc', {path: '/node/socket.io'});

And I receive message with

Error during WebSocket handshake: Unexpected response code: 400

I don't want to use in node server namespace, but only route all data from url with /node to port 5000.

Thanks for help

1
Which version of apache? Have you taken a look at this: httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html I would first suggest that you get rid of the ws:// in your path and not use connect: var socket = io({path: '/node'}); should be enough - Quentin
I use apache: 2.4.7. I didn't test this apache mod. I will try - Robert

1 Answers

0
votes

The formatting of my comment does not look too good, so I'll take a few guesses and try to answer your request for assistance.

My guess is that your are using apache 2.2 which is not too aware of the ws protocol. socket.io, however is still capable of failing over AFAIK.

So in the end, you should be able to get your setup working despite the presence of the aforementioned error.

For that, as stated in my comment, I would suggest you change your io.connect as follows:

var socket = io({path: '/node/socket.io'});

You will still get this error, but your server and client should still be able to communicate.

Next, if you wish to get rid of this error, I would suggest the use of mod_proxy_wstunnel, which is available for apache 2.4 (apparently some people managed to build it for 2.2 as well so you may try that too)

Update: looks like the newer socket.io has changed the path for http and ws (used to be different, but are now based on the same path followed by a specific query) Look at this thread that discusses just that: https://community.rackspace.com/developers/f/7/t/3477 It appears that nginx has better support for websockets and should be the prefered solution here. It might be worth looking at ways to detect the "transport" parameter in the socket.io query in order to let apache know which protocol to use for the ProxyPass.

EDIT: correct the path, add some info.