Broadcast data from server when updated

I want to develop a site in php where if any online clients updates any data from the db it has to be broadcasted to all online users.
It can be similar to chat system
So is it better to use long poll or web socket or is there any other better mechanism to do so.
We are currently using short poll but have to wait for it( over here its 5 sec) to get executed.
I want to broadcast the data as soon as there is any update in db it should trigger the script and data should be reflected at clients end

OS: centos
LVL 15
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You're running up against one of the design factors of the WWW, and it relates to security in a way that makes it pretty difficult to circumvent.  The nature of the HTTP protocol is described in this article.  Before you read the rest of this response, please take a moment to be sure you understand what the article is saying about how the protocol works.

As a practical matter, if you really want to broadcast, you cannot use the WWW; you would have to use something that does not use HTTP client/server protocols.  But you may be able to simulate the broadcast adequately with a request/response mechanism.  Most web-based chat systems use something like this.

Each client has a timer-driven event handler that makes an AJAX request to a simple script on the server.  The client sends the last-updated code to the server.  This code is made from the md5() string of the last time an eligible update was made.  By eligible, we mean an update that needs to be broadcast.

If the client sends a null string or a last-updated code that does not match the code on the server, the server responds with the broadcast information.  This is obviously a "heavy" response, and it is therefore to be avoided except when it is actually needed.  Part of the server's response is the new last-updated code; the client stores this code and uses it for the next timer-driven request.

If the client sends a last-updated code that matches the master code on the server, you know that the client is in sync with the server and there is no new data to broadcast, so the server's response is instantaneous and empty.

When a client posts information that is eligible for broadcast, the server stores the information and updates its copy of the last-updated code.  As a result, the next request from any client will have a last-updated code mismatch and the server will respond with the new eligible information.  This will create the appearance of a server broadcast that is contemporaneous with the new post of eligible information.

Another way of thinking of this application is that it, in effect, uses a client-side cache to store the old eligible information and only updates the cache when the cache key has a mismatch.

You can set the client timer to drive the event handler once every second and you will achieve apparent broadcast speed without having to go around the HTTP C/S protocol.  You may be able to slow the timer down to once every two seconds (IIRC, Google uses two seconds and nobody complains).  If you try to get the timer below one second, you will find these things: (1) you will unnecessarily raise the load on the server, (2) nobody will notice the faster speed, (3) speed will become variable as the timer starts firing faster than the HTTP protocols of the internet can send timely data.  If you set the timer above 5 seconds, you may begin to notice latency.  I would start with one or two seconds and adjust the timer after you have some measurements and client feedback.
InsoftserviceAuthor Commented:
what about long poll and Server-Sent Events can't it be done by that.I don't want to use node into these project. two second suggestion is good would be good
Ray PaseurCommented:
I don't see anything wrong with Long Polling, except that an Apache server might have a hard time with all the open connections.  I've never used server-sent events.  Some references indicate compatibility issues with IE - not sure about that.
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

greetings insoftservice, , You seem to think that in the web browser, you can do "Server-Sent Events", but I have Never seen that done, (with a web browser that only makes REQUESTS). However you can do that with cell-phones, but it dose NOT involve a web browser, it uses the cell-phone connection. Google and others offer a "Service" that a PHP can send a request to with POST data, , and that service will send an android sent event to the registered cell phone recipients.
I tried to do a "Long Polling" connection with browser and PHP,  - BUT - It was not workable in so many situations, and PHP is NOT a server system that can be effectivly set up for long polling, constant connections. At least that's my opinion and experience. There is a new tech for "javascript browser SOCKET connections", however different browsers have different set-up and abilities on that tech, and again, I could not effectively dependably get it to work on different browsers and different servers, a couple of years ago, but maybe the new browser versions can do better?

I agree with Ray, that the most dependable option is AJAX, However if you have a large amount of users at the same time, it CAN and will over-whelm your Database through-put, with a large amount of rather useless data-base requests, if you just do this like you would a regular PHP "Post" single request page.
Ray PaseurCommented:
InsoftserviceAuthor Commented:
Thx for your comments
As commented by Ray and Slick812 i would use ajax to fetch the data after every 2 sec interval i.e by using short poll.
Do node js would help in this case.If i am not wrong now a days people uses node js for chat system for displaying the online status and users message.
Ray PaseurCommented:
Two final thoughts...
if you have a large amount of users at the same time, it CAN and will over-whelm your Database through-put,...
Not necessarily.  The common design pattern to reduce unnecessary server-side workloads is cache.  Here is how it's done.
people uses node js for chat system...
You might want to try it.  There are lots of links that come up when you make a search for "Node.js on Centos."  Here is one.  I did not consider Node because the question said, "I want to develop a site in php..." was posted in the PHP and jQuery topic areas.
? ? ?, OK , Node javascript is a server side implementation that is used instead of a JAVA, PHP, or ASP as the server side language. So This is NOT a PHP framework add-on, but requires a Sever that is set up to run the Node js as it's HTTP request operation language instead of (not in addition to) PHP or any other language. There are many "Raves" now that developers are calling Node js a vastly superior sever efficiency for real time web browser GUI complex interactive applications and other things. There can be a big advantage over high latency server responses such as ASP and PHP for certain browser - server operations. However, it is my view, that you will need to study - analyze what your code techniques and progressions require for you to handle the server side requierments (request numbers and database connections) and simultaneous  user numbers might be for your pages to have the "Updating" you feel you need. , , Here is a Link to a page that compares Node js and PHP -

He does his performance "Test" for Node and PHP, and for 350 concurrent users or less both are fast, BUT as you get past 400 concurrent users, , PHP chokes, and gets slower and slower with additional requests. It is a well established Fact that PHP can not handle ajax repetitive polling for more than about 400 users, due to the way PHP has to do independent "initialization" steps for each page request. However, PHP can and does handle 400 and more requests well, but NOT repetitive continuous Ajax requests.  

Just for conversation here, A method that you might consider , that I have used to good effect, is to NOT query a php page in the Ajax polling repetition, you poll a plain text file on the server, with a UNIQUE line of text (a php time stamp for example), and if That text line changes, then get the Ajax to request a one-time call to a PHP page that gets the Database changes needed for update back to browser.
You will need to add a method to the PHP page that Changes the Datbase table (updates it) so that it writes a new time stamp to the polling text file.

BUT, there is MUCH that your programming does or does not need to do that may NOT be able to use this text poll, as I said it's more complicated depending on what's needed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InsoftserviceAuthor Commented:
Thx for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.