Event Driven Front End On Yii


I’m wondering what would be the best way to do user event driven front ends in Yii

what I mean is : let’s say many people are on one same page (=url)

if a status of something is changed by a user

how to make it refresh to everyone

I know the ajax pinging technique but it’s so slow, and generates constent pinging requests

I also looked into websockets which is far from being a standard and unavailable on all browsers for the moment

which brings me to the Forum

can it be done smoothly using Yii ?

or should I look into other frameworks like nodeJS

I’d prefer to stick with Yii since my whole application is on Yii

Thanks for the insight


Hi Tibor

You could create a function called onBeginRequest and set a variable according to sql data

this sql data will be set when user status changed

If there way an easy solution for this, I guess we’d see much more really nice websites. ;)

From my point of view, what you need to do is to define an abstract notification mechanism, that allows your server to send messages to the clients. I’d probably go with an observer pattern, defining the subject as an interface in your yii app. The observer would be a javascript class, for simplicity with the common “notify” method that is (conceptually) called by the subject when something happened. This way, you could get events from the server to your connected clients (all the browsers that visit the page).

Every connected client might implement a similar pattern to re-distribute incomming messages to interested components of client side logic, but this has no longer to do with passing messages from server to client…

My point is: as soon as you have established the concept of passing messages from server to client, and you have a defined interface, everything else becomes an implementation detail.

If you have to to stick with http, the server can’t push to the clients. So your subject implementation might store the events in db (together with connected client IDs). The javascript observer implementation would poll (maybe implement long polling/ comet) and receive the messages stored in DB (message/clientID tuple can be deleted after they have been requested).

If you have clients that know how to work with websockets, you can provide a second implementation of your subject, that doesn’t need to store messages for later retrieval, but simply pushes them to the bleeding edge client.

The benefit is, that the matter of how the message finds its way to the client is transparent to your yii application. It only works with the subject interface you defined in the first step.

So in a nutshell: Yii probably won’t do much to help you accomplish the message passing piece of work. But it can be implemented and if done right, it can be re-used as an extension.

Oh, and I’d love to hear some more opinions on that topic. :)