Seth
Seth

Reputation: 2173

Checking when new SQL rows are inserted

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

All I need to do is have the client 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?

Upvotes: 1

Views: 267

Answers (3)

Joe Phillips
Joe Phillips

Reputation: 51150

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

Upvotes: 2

Anorgan
Anorgan

Reputation: 303

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.

Upvotes: 0

smdrager
smdrager

Reputation: 7417

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.

Upvotes: 0

Related Questions