开发者

Checking when new SQL rows are inserted

开发者 https://www.devze.com 2023-02-21 20:35 出处:网络
I am working on a website, and I need to have real-time updates.. All I need to do is have the c开发者_如何学编程lient echo the row when it is added at the exact time..

I am working on a website, and I need to have real-time updates..

All I need to do is have the c开发者_如何学编程lient echo the row when it is added at the exact time..

I am pretty good in PHP, and I can do simple queries in SQL..

What do you suggest?


I'm pretty sure you're going to have a hard time getting around polling for this type of scenario. You will probably have to continually poll the data store.

Tips: Use server side caching and use ajax


You're looking for a comet-like system. I'm familiar with a comet-clone... it uses a table for subscriptions and pushes. In your case you'd want to use a stored procedure. In the stored procedure, when an insert is done to that table and it is successful, it would insert into the comet message table. A service will checking this message table every second (or however often you want). Once it sends a message, it remove the message from the table.

This extra table removes all the overhead off of the table you are inserting into, and instead puts it onto the comet message table which should never have more than a handful of rows to report, since it is being wiped after the messages are reported.


What you are looking for is a method of subscribing to a channel. You can accomplish this with Push (Long poll) - so when the page loads, you request the new items. Server then does the loop every 1 second for 30 seconds. If it finds new rows, server spits them out, and you do another ajax request with different timestamp.

Other technique is WebSockets where you get to subscribe to a channel and you're done. But to implement it could be harder than the first method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号