Re: Periodic Actions in PHP?

[email protected] (Colin Guthrie)
Newsgroups php.general
Message-ID <[email protected]>
'Twas brillig, and Parham Doustdar at 13/06/09 09:42 did gyre and gimble:
> Hi there, I'm going to create a small chat script with PHP. The
> messages you want others to see will be added to a flat file (I.E.
> TXT file), and read and displayed by PHP. However, I want this
> reading and displaying to be periodic. This means that I want PHP to
> check the file for new lines every,say, fifteen seconds. How may I do
> that? I have been unable to find any function that acts like a timer.
> 
For handling periodic tasks in a web environement, I use a little
persistent object (stored using APC) that has it's "process()" method
called on every request. Internally it knows when it was last run and
will actually only do work in it's process() method if it's not been
called for a while.

When this work is done, the user's request is obviously slowed down
slightly, but not in a noticeable way.

If you don't need something this self contained, then a normal cron job 
is needed.


All that asside, I'm not sure you're asking the right question. If 
you've got a chat system, then the client side is presumable a web 
server. You can't really push information from the server side to the 
client, so you really need to pull it from the client side. Usually 
you'd have a javascript timeout that submits an ajax request to the 
server and displays any new messages to the user. In this case, PHP 
doesn't need to be timed, the client is pulling periodically so it's the 
client that sets the timer.

Col

-- 

Colin Guthrie
gmane(at)colin.guthr.ie
http://colin.guthr.ie/

Day Job:
   Tribalogic Limited [http://www.tribalogic.net/]
Open Source:
   Mandriva Linux Contributor [http://www.mandriva.com/]
   PulseAudio Hacker [http://www.pulseaudio.org/]
   Trac Hacker [http://trac.edgewall.org/]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.