n14charlie Posted May 23, 2009 Share Posted May 23, 2009 Hey I wanted to know what is the "pushing" solution using PHP when a client needs to wait for an update and needs to do alot of polling? One option I did was looping in the PHP script until an update is needed but is there a better solution? Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/ Share on other sites More sharing options...
Daniel0 Posted May 23, 2009 Share Posted May 23, 2009 Push technology is a bit more difficult to pull off (no pun intended) on the HTTP protocol because it's stateless. One way of doing it is using long polling. Essentially you make a request (using JavaScript) as normal to the backend server, but if there is no information available then it'll just wait until there is. Once information becomes available the request will complete and a new one will immediately be made. That is probably the easiest way to implement it. Depending on the frequency of new information to be pushed, and the importance of receiving all message on the client end, you might have to take network latency into consideration and give each message a sequentially incremented ID. The client can then pass the last ID received while establishing a connection so the server can send all messages that was sent since the last time. Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840686 Share on other sites More sharing options...
ohdang888 Posted May 23, 2009 Share Posted May 23, 2009 One option I did was looping in the PHP script until an update is needed but is there a better solution? Don't do that. PHP can run a loop millions of times in an amount of less than 10 seconds(depending on what's in the loop), so that method can cause excessive server load Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840696 Share on other sites More sharing options...
Daniel0 Posted May 23, 2009 Share Posted May 23, 2009 You are aware that the browser you used to post that message is kept open because of infinite loops, right? Otherwise the window would have closed immediately after opening. Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840700 Share on other sites More sharing options...
ohdang888 Posted May 23, 2009 Share Posted May 23, 2009 You are aware that the browser you used to post that message is kept open because of infinite loops, right? Otherwise the window would have closed immediately after opening. but not infinite php loops, right? Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840732 Share on other sites More sharing options...
n14charlie Posted May 23, 2009 Author Share Posted May 23, 2009 -Is it better to make one php script run in a loop until new data is found or keep calling a php script until the information is found?(polling vs long polling) - the server I used returned a message php execution time is max 20 sec.. are there even servers which let your php run "set_time_limit(0)"? -and what's the technique "real" web applications use? is it a complex push techology thanks for the responses Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840749 Share on other sites More sharing options...
Daniel0 Posted May 23, 2009 Share Posted May 23, 2009 You are aware that the browser you used to post that message is kept open because of infinite loops, right? Otherwise the window would have closed immediately after opening. but not infinite php loops, right? And what is the alleged difference between an infinite loop in PHP, C, C++, C#, Java, Python, etc.? -Is it better to make one php script run in a loop until new data is found or keep calling a php script until the information is found?(polling vs long polling) Waiting for the server is normally called "push" and asking the server "is there anything new yet" is called "pull". Push will generally lead to better performance. Take one of the desktop IM application you might be using for instance. Imagine what a massive load it would put on servers if you constantly had to ask the server if someone IM'ed you. It would be much better just connecting to the server and say "when there are any messages for me, please send them to me" and then just wait. - the server I used returned a message php execution time is max 20 sec.. are there even servers which let your php run "set_time_limit(0)"? I don't know about shared hosting. I would imagine that most don't allow that. You could look into purchasing a VPS (or even a dedicated server if your requirements warrant it) where you can configure PHP as you see fit. -and what's the technique "real" web applications use? is it a complex push techology I don't know what "real" web application use (whatever that's supposed to mean). I believe there is also a technique with using an invisible Flash app, and something with refreshing iframes. You should easily be able to find further information using Google. Long polling is probably the easiest to implement, and it is very widely supported. To prevent getting the connection closed prematurely you might want to send small payloads to the listening servers once in a while though. Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840758 Share on other sites More sharing options...
ohdang888 Posted May 23, 2009 Share Posted May 23, 2009 And what is the alleged difference between an infinite loop in PHP, C, C++, C#, Java, Python, etc.? I'm not an expert by any means, this is just what i always thought... PHP is on the server, shared by many people. Infinite loops in php(as far as i know) are very server intensive, as they continue until the script times out. Infinite loops, with many users causing this to happen at the same time, would hog resources and slow the server up. also, another question...is the html echoed by the php script not sent through the internet to the user until the end of php script is reached? Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840773 Share on other sites More sharing options...
DarkSuperHero Posted May 23, 2009 Share Posted May 23, 2009 i believe its sent as its parsed.....it would only be held back if you were using output buffering I believe...I could be wrong...anyone ? Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840794 Share on other sites More sharing options...
Daniel0 Posted May 23, 2009 Share Posted May 23, 2009 "It" (HTML/JSON/XML/whatever) is sent whenever there is something to send. Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840813 Share on other sites More sharing options...
RussellReal Posted May 23, 2009 Share Posted May 23, 2009 actually, what would be cool, (which I've created something similar to this), instead of AJAX, you set up a 1 x 1 px flash app, and inside the app you connect to a server (I made my server in PHP), send the flash policy file and everything else, THEN that connection should be kept alive, thus, when an update becomes available, you just send the update to every connection that is connected, and that is as real time as you can get, without any kinda lag for the end user -- sorry, extra info.. I connected the server and user via a socket, but it probably wasn't hard to get the correlation Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-840901 Share on other sites More sharing options...
DarkSuperHero Posted May 24, 2009 Share Posted May 24, 2009 flash can also communicate with JS so then you can manipulate the DOM Quote Link to comment https://forums.phpfreaks.com/topic/159379-pollingpushing/#findComment-841264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.