change in database will automatically be displayed in web interface How can you do such a thing!
I would like to know how the news or new event in Facebook page a开发者_运维技巧utomatically inserted as event or , I work as a programmer, I only know that we get the data from database using ajax without reload function, but vice versa, a change in database will automatically be displayed in web interface How can you do such a thing! by ajax! or by trigger in here or what! I ask for help
Comet keeps a connection open and allows you to push data to the client. You are essentially posting the data to your server (via ajax or regular), and trigger a response on the server (usually by writing in a file).
The response is sent to all open client connections.
See Comet and jQuery for more info.
I assume you mean functionality such as the popup notifications where you are automatically notified when someone has commented etc. (i.e. a comment about you has been added to the database).
There are a number of solutions to do this: "polling", "long polling" and evented I/O such as node.js
The Ajax approach to the problem is to have the page load, but once it has done so - invoke some script that will periodically go and load a file and get results from it, which may or may not contain something you can update.
Look at setTimeout and setInterval commands.
This could be a wasteful approach though if updates are infrequent and you are making requests every few seconds to see if anything changed - upscale it to 1000 concurrent users and thats a lot of requests flying about!
Try it yourself: Set up a plain page with a couple of DIVs, use JQuery to fire out an ajax after 2 minutes, or every 30 seconds, and have that ajax return a random number, or colour code - something to change your page (it doesnt even need to be database driven).
See it on other pages: Another way to observe the effect is using something like HTTPFox (firefox add on) on one of these pages, you'll see requests going out periodically to fetch updates, and returning data which can then be used in your page however you like.
I don't know how Facebook does it, I know how I'd do it with LAMP.
There are two options - using emulated full duplex communication (or server PUSH if you will) and using actual full duplex communication (WebSockets protocol).
First one will work in older browsers too. Basically, at certain intervals you query the database. Using Comet or regular long polling you simply check whether there are new entries that are of interest for the currently browsing user. Downside: inefficient and not actual real-time.
Second one will work only in newer browsers (I don't know whether there's a browser that implements WebSockets protocol except Chrome). What happens is that there's a data-pipe going 2 ways. Your server can now feed the client with info without client needing to request that data. Upside: quick, doesn't waste bandwith, no overhead, simple to use and implement. Downside: works only in small amount of browsers, protocol not complete yet.
After you've enabled full-duplex communication between client (browser) and server, it's time to start sending data!
Again, there are two ways - periodically query the database or actually enable MySQL to execute external programs. I prefer executing external programs but that has security implications that I won't go into at the moment.
MySQL doesn't have the capability of telling some external program something happened that changed data. That's why we can extend it with User Defined Functions.
One such UDF that's of interest sys_exec.
So to cut it short, using sys_exec and WebSockets:
MySQL table receives data. A trigger is invoked, trigger contains. sys_exec('your_program_that_dispatches_changes_around'). The program responsible for dispatching data is invoked and it collects the data that needs to be sent to all connected clients (browsers). The program dispatches the data.
You got the real-time full-duplex solution.
It's kind of tricky to implement, so using Comet and regular AJAX calls is the simplest and fastest way to achieve the functionality you want.
精彩评论