Page 1 of 1

sleep func in php

Posted: Sat Oct 22, 2011 5:46 pm
by motiksir
hi
i try to do a virtual chat..
and i want to do a sleep func before that i accept answer..
for example:
you:hi
(sleep 3 sec)
virtual chat:hi my friend


i try to do the functions

function wait(msecs){
var start = new Date().getTime();
var cur = start
while(cur - start < msecs){
cur = new Date().getTime();
}
return false;
}



function sleep(milliSeconds){
var startTime = new Date().getTime(); // get the current time
while (new Date().getTime() < startTime + milliSeconds); // hog cpu

but it return the answer Immediately without wait..
so how to repair it?

thanks before

Re: sleep func in php

Posted: Sat Oct 22, 2011 6:01 pm
by twinedev
THe thing you have to remember is PHP is SERVER-SIDE executed. This means that it will not interact with the used EXCEPT for the entire script being called by the user, and its output sent to the browser. What the user does with the output in the browser, PHP has no concerns with.

You will need to look into javascript (CLIENT-SIDE) to interact with the user's browser and what they do with it.

Also, if you are planning anything but other than the basic chat, you may be better off looking for a provided service.

If you are the moderator of the chat, and you have 4 people you are chatting with, that means that every 3 seconds, you have 5 calls to the web server, which logs all 5 calls to log files (apache anyhow), if you are doing any type of database interaction, that is 5 calls MINIMUM to the database every three seconds... A lot of overhead for a chat system. In 10 minutes time, you have 1000 entries into your log file, 1000+ connections/queries to the database....

-Greg

Re: sleep func in php

Posted: Sat Oct 22, 2011 11:40 pm
by motiksir
Im sorry,
But i dont understand the problem if i have 5 logs request info from DB??

Re: sleep func in php

Posted: Sat Oct 22, 2011 11:52 pm
by twinedev
It is called server load, every page call (again 100 calls per minute for 5 users) uses up resources, produces file interaction (log files, reading the php script, session files, each table accessed is a file read.... On a lone server doing nothing else, not so bad, but you get a server with a high load (use), the more sloppy this is written, the more load it puts on the server and impact it has on other sites/scripts running on the server. (Especially on cheaper servers that have a slower file system)

Plus, I just took one of my sites, grabbed a random 100 lines from the access log file, and for 100 lines, what 5 users would generate per minute, that is 27,000 bytes of storage on the server ate up! again, PER MINUTE for just 5 users.