Jump to content

polling/pushing


n14charlie

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

-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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.