Real-time web apps in PHP

Hi guys!

Currently I’m developing a web app for online collaboration. One of the key features is real-time responsiveness - i.e. when someone posts something, others get the new post instantly.

I do not consider old-school stuff like setting timeout on a client. At the moment I think that socket.io is pretty much what I need. But I have a doubt whether the tandem "php+socket.io" is viable. For example, how do I make them communicate with each other? How do they share the auth session?

Does anybody have experience in this field? Your experience is very valuable to me.

Hi tedd,

To communicate between php and web sockets I suppose you will need some comet solution. We did it on this way:

  1. Have Redis (pub/sub feature) to send/retrieve messages (channel No is user ID)

  2. Have PHP to send notifications into Redis when something happen

  3. Have Node.js app listen all channels and send notification to web sockets

How to identify user:

  1. After user login we added to Redis hash pair: sessionID => userID

  2. After user connected to socket, it send auth command to node.js with session ID.

  3. Node.js check hash in Redis for sessionID and get userID.

  4. Node.js subscribe connection to userID channel

That’s very interesting, thanks!

Just to clarify: do you use pure Node.js or Node.js plus socket.io (or something)?

How stable the whole system is? How many simultaneous connections it can handle?

Also, do you proxy your nodejs server?

Node.js + socket.io

Have no serious problems.

Honestly, can’t tell you exactly because we haven’t opportunity to run with highload yet. But I assume we wouldn’t have any problems with ~100 connections (or maybe even with 1k, because node.js + redis is very fast).

Also please note, that you could easy to scale node.js application by just copping to another node. Redis also is fast & scalable engine. So I don’t think that any deadlock issues will come. But this technology is still “beta” and no guaranties here.

We no need to use proxy, so we connect directly to nodejs (we use 3001 port for this). Also nodejs didn’t call any php code (for example, through fastcgi) and so on. To keep it fast as possible.

Many thanks for answers! You really clarified things.

As for proxying nodejs - I’ve heard that it is recommended to proxy nodejs by a stable web server (nginx for example) for security reasons. Have no proofs though.

I’m planning on building a small web server using nginx and node js, since I need it for building a real-time web application (with Yii, of course), as soon as my dedicated computer arrives.

I was thinking of nginx to process php and nodejs to send database queries and updates.

I will post my development results and experiences here, when I get them ;)