0

I have an existing PHP app running on Apache server.

Question, is it possible to embed say Socket.IO Client-side JS by "Proxy-Pass"-ing to NodeJS server?

Say, I save a key-value pair with PHP in DB, and simultaneously send that message to everyone connected to that channel, the value I just saved.

If I was using NodeJS and Socket.IO/Faye I would embed the client-side JS in the pages served by Apache. But essentially that script comes from Proxy-Pass to NodeJS.

Some light on this would be very kind.

Thanks

1
  • Possible to use Redis PubSub? Commented Jul 22, 2013 at 12:09

2 Answers 2

5

There is a similar question here:

Accessing socket.io server via Apache served pages

You can achieve this using Apache, but I think it would be better to either connect to Socket.IO directly or use something better for proxying like HAProxy: HAProxy + WebSocket Disconnection

I would choose HAProxy and use a port such as 4000 for example especially for Socket.IO and port 843 for Flash-WebSockets (Socket.IO will check if WebSockets are available in the browser and if they're not SIO can change the transport to Flash-WebSockets; providing your transport order is WS native, WS Flash, etc): https://gist.github.com/1014904 (check HAProxy's config there for port 843).

The reason for choosing HAProxy would be that I could use multiple processes across multiple machines when I need to handle big traffic, and HAProxy is very good at that.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thank you for your prompt reply. Is it possible to show how to configuring HAProxy for this purpose?
Take a look at: stackoverflow.com/questions/4360221/… You'll also need to add the flash stuff from here: gist.github.com/1496429
Thanks a ton! Socket.IO is working perfect with the above setup!
0

The first thought is to make a Socket.io client, well it's not that hard except that you'll be busy hacking the Socket.io protocol which is not standard (WebSocket is a standard). I've done something like this in the past but it was a waste of time.

Since Node.Js has a shared scope (The Global Scope) and it's mostly a single process application, you can always make a dedicated HTTP server for non-realtime interacting with Socket.io:

var app = require('http').createServer(function S(req, res){
    res.writeHead(200, {
        'Content-Type': 'text/html'
    });

    res.end('');


    var i;
    var sockets = io.sockets.sockets;
    for (i in sockets) {
        if (sockets.hasOwnProperty(i)) {
            var socket = sockets[i];
            // you have res, and socket so so something with it!
            socket.emit('myevent', {msg: "json is cool :)"});
        }
    }

});

var io = require('socket.io').listen(app);
app.listen(80);

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

    socket.on('theirsevent', function (data) {

    });
});

IMHO this is way better that modifying apache with even more modules. This could even work with Python and .Net since it's HTTP.

I think this is much simpler/cleaner that any other solution. ... Unless there is some use case that this solution does not fit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.