Reputation: 103
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
Upvotes: 0
Views: 5018
Reputation: 5160
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.
Upvotes: 0
Reputation: 63683
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.
Upvotes: 5