lots_of_questions
lots_of_questions

Reputation: 1149

PHP / MySQL auto update clients

I'm writing a website using PHP / MySQL. The website should allow users browsers to be automatically updated when a table is updated in a database (which is caused by someone inserting something though the PHP site). Basically, if the state of the table changes other users on the website should immediately see the changes.

I'm relatively new to PHP / AJAX and other web technologies. The only way I can think of doing this is to have the clients manually recheck the database every second or two via a timer (but this seems like a lot of wasted bandwidth). Is there some way to have the users automatically notified when the database changes?

Thank you!

Upvotes: 0

Views: 1036

Answers (2)

MrCode
MrCode

Reputation: 64526

1) AJAX Polling. As you say, use ajax to call the PHP on the server side to get updates.

2) HTML5 Websockets. Websockets are a new feature of HTML5 which allow the browser to keep a bi-directional TCP connection open to the server, where data can be transmitted both ways without polling. Currently this isn't as widely supported as ajax polling is.

3) Java Applet/ActiveX control. Generally the most undesirable solution because of the user having to install third party software.

Upvotes: 0

Gilly
Gilly

Reputation: 9692

You already had the solution in mind. You can use ajax to automatically check for changes.

It's not possible for the server to contact you when something has changed. It doesn't have to be bandwidth intensive when you lower the retrieval intervals. You can have Ajax check for a blank page with as little as possible text. That way the bandwidth usage wont be too bad.

Facebook does it this way too.

Upvotes: 1

Related Questions