Re: [PHP] Re: about php comet

2011-06-03 Thread Adam Richardson
On Fri, Jun 3, 2011 at 1:50 AM, 李白|字一日 calid...@gmail.com wrote:

 any idea?

 2011/5/25 李白|字一日 calid...@gmail.com

  hello,
 
  I am every interested in comet applications recently, and sadly found php
  is very weak in this area.
 
  i am just wondering if it is possible to write an extension to extend the
  ability of the php to easy the way to comet applications?
 
  if it possible for php to hold the connect from the client when it is in
  fast cgi mode or apache mod php mode?


Comet can use one of several techniques for low-latency data (
http://cometdaily.com/about/), with the long-polling mechanism to simulate
the realtime pushing of data from the server being a popular option:
http://cometdaily.com/2007/11/15/the-long-polling-technique/
http://en.wikipedia.org/wiki/Comet_(programming)

http://en.wikipedia.org/wiki/Comet_(programming)To my understanding, PHP
itself can accommodate this quite nicely. Merely start an infinite loop that
breaks and returns new data when available. You'll probably have to adjust
your web server settings and php.ini configuration to allow reasonably long
requests for this particular need. However, when requests do time out, it's
not a big deal as the javascript should be set up to reinitiate a new
request if that happens, just as it would if the request returned new data
and closed, giving the appearance of a continuous stream of pushed data.

This all said, if you wanted to write an extension to facilitate long
polling, you could, but given the natural latencies for these requests, I'm
not sure you'd find a significant benefit.

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


Re: [PHP] Re: about php comet

2011-06-03 Thread 李白|字一日
is there an efficient way to hold the requests while loop is an expensive
way in most cases.

and i don't know how to notify the holding connections if the change need to
be notify to the holding requests?

writing extension to php is also a very expensive way for me:(

2011/6/3 Adam Richardson simples...@gmail.com

 On Fri, Jun 3, 2011 at 1:50 AM, 李白|字一日 calid...@gmail.com wrote:

 any idea?

 2011/5/25 李白|字一日 calid...@gmail.com

  hello,
 
  I am every interested in comet applications recently, and sadly found
 php
  is very weak in this area.
 
  i am just wondering if it is possible to write an extension to extend
 the
  ability of the php to easy the way to comet applications?
 
  if it possible for php to hold the connect from the client when it is in
  fast cgi mode or apache mod php mode?


 Comet can use one of several techniques for low-latency data (
 http://cometdaily.com/about/), with the long-polling mechanism to simulate
 the realtime pushing of data from the server being a popular option:
 http://cometdaily.com/2007/11/15/the-long-polling-technique/
 http://en.wikipedia.org/wiki/Comet_(programming)

 http://en.wikipedia.org/wiki/Comet_(programming)To my understanding, PHP
 itself can accommodate this quite nicely. Merely start an infinite loop that
 breaks and returns new data when available. You'll probably have to adjust
 your web server settings and php.ini configuration to allow reasonably long
 requests for this particular need. However, when requests do time out, it's
 not a big deal as the javascript should be set up to reinitiate a new
 request if that happens, just as it would if the request returned new data
 and closed, giving the appearance of a continuous stream of pushed data.

 This all said, if you wanted to write an extension to facilitate long
 polling, you could, but given the natural latencies for these requests, I'm
 not sure you'd find a significant benefit.

 Adam

 --
 Nephtali:  A simple, flexible, fast, and security-focused PHP framework
 http://nephtaliproject.com



Re: [PHP] Re: about php comet

2011-06-03 Thread Adam Richardson
On Fri, Jun 3, 2011 at 2:20 AM, 李白|字一日 calid...@gmail.com wrote:

 is there an efficient way to hold the requests while loop is an expensive
 way in most cases.


You can call sleep(number_of_seconds_to_sleep) within the while loop to
lower the cost, so to speak.


 and i don't know how to notify the holding connections if the change need
 to be notify to the holding requests?


Sorry, I'm not sure what you mean here.

Let's say you were checking a db table for new rows. When a new row
appeared, you'd break out of the loop and return the data to the page with
the javascript that initiated the request (maybe you're PHP is returning
JSON, XML, or an HTML fragment for the javascript to manipulate.) The
javascript would update the page content and then initiate a new
long-polling request and wait for new data.

writing extension to php is also a very expensive way for me:(


I agree with you. That's in part why I suggested that the extension wouldn't
be needed or worthwhile in this case and basic PHP would be a reasonable
solution.

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


Re: [PHP] Re: about php comet

2011-06-03 Thread Adam Richardson
On Fri, Jun 3, 2011 at 3:43 AM, 李白|字一日 calid...@gmail.com wrote:



 2011/6/3 Adam Richardson simples...@gmail.com

 On Fri, Jun 3, 2011 at 2:20 AM, 李白|字一日 calid...@gmail.com wrote:

 is there an efficient way to hold the requests while loop is an expensive
 way in most cases.


 You can call sleep(number_of_seconds_to_sleep) within the while loop to
 lower the cost, so to speak.

 yes, an interrupt will be a better way. currently libevent is in beta
 state, and i don't know if the libevent extension can be used in mod_php to
 implement comet applications.


Oh, I see. Yes, I'm not sure I'd try using that extension yet.




 and i don't know how to notify the holding connections if the change need
 to be notify to the holding requests?


 Sorry, I'm not sure what you mean here.


 please forgive my poor english, i mean we may have projects need
 interaction with mulitple users and the state changes of one user should be
 send to the users involved, like online games chatting rooms.


I believe I understand you, now. This could get costly in terms of
resources, and I now better understand your interest in the libevent
extension.

I'd be tempted to write custom C extensions for a web server like nginx,
which naturally handles asynchronous IO. And, in this case, you could even
make use of fast polling instead of long polling, due to the performance of
nginx, such as demonstrated in the this example:
http://amix.dk/blog/post/19414

And, don't worry about your English too much. My Chinese stops after ni hao
;)

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com


Re: [PHP] Re: about php comet

2011-06-03 Thread Stuart Dallas
On Fri, Jun 3, 2011 at 7:11 AM, Adam Richardson simples...@gmail.comwrote:

 On Fri, Jun 3, 2011 at 1:50 AM, 李白|字一日 calid...@gmail.com wrote:

  any idea?
 
  2011/5/25 李白|字一日 calid...@gmail.com
 
   hello,
  
   I am every interested in comet applications recently, and sadly found
 php
   is very weak in this area.
  
   i am just wondering if it is possible to write an extension to extend
 the
   ability of the php to easy the way to comet applications?
  
   if it possible for php to hold the connect from the client when it is
 in
   fast cgi mode or apache mod php mode?
 

 Comet can use one of several techniques for low-latency data (
 http://cometdaily.com/about/), with the long-polling mechanism to simulate
 the realtime pushing of data from the server being a popular option:
 http://cometdaily.com/2007/11/15/the-long-polling-technique/
 http://en.wikipedia.org/wiki/Comet_(programming)

 http://en.wikipedia.org/wiki/Comet_(programming)To my understanding, PHP
 itself can accommodate this quite nicely. Merely start an infinite loop
 that
 breaks and returns new data when available. You'll probably have to adjust
 your web server settings and php.ini configuration to allow reasonably long
 requests for this particular need. However, when requests do time out, it's
 not a big deal as the javascript should be set up to reinitiate a new
 request if that happens, just as it would if the request returned new data
 and closed, giving the appearance of a continuous stream of pushed data.

 This all said, if you wanted to write an extension to facilitate long
 polling, you could, but given the natural latencies for these requests, I'm
 not sure you'd find a significant benefit.


One word of caution... While PHP can be used to implement long polling, bear
in mind that PHP is fairly expensive in terms of resource usage per
connection. Unless you're swimming in servers or only expecting a few
concurrent connections at any one time, I'd recommend using something that
holds multiple connections per process, rather than one process per
connection.

IMO, web-based PHP is not the right solution to this problem. You could
write a select-based comet server in PHP, but it definitely wouldn't be in
my top 10 language choices.

I've just seen a later email in this thread that mentions that this is a
chat-type system. Watch this and have your mind blown...
http://vimeo.com/20605470 - at some point he describes how to write a pretty
efficient chat server in PHP using ZeroMQ, but the whole thing is worth
watching.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/


Re: [PHP] Re: about php comet

2011-06-03 Thread 李白|字一日
2011/6/3 Adam Richardson simples...@gmail.com

 On Fri, Jun 3, 2011 at 3:43 AM, 李白|字一日 calid...@gmail.com wrote:



 2011/6/3 Adam Richardson simples...@gmail.com

 On Fri, Jun 3, 2011 at 2:20 AM, 李白|字一日 calid...@gmail.com wrote:

 is there an efficient way to hold the requests while loop is an
 expensive way in most cases.


 You can call sleep(number_of_seconds_to_sleep) within the while loop to
 lower the cost, so to speak.

 yes, an interrupt will be a better way. currently libevent is in beta
 state, and i don't know if the libevent extension can be used in mod_php to
 implement comet applications.


 Oh, I see. Yes, I'm not sure I'd try using that extension yet.




 and i don't know how to notify the holding connections if the change
 need to be notify to the holding requests?


 Sorry, I'm not sure what you mean here.


 please forgive my poor english, i mean we may have projects need
 interaction with mulitple users and the state changes of one user should be
 send to the users involved, like online games chatting rooms.


 I believe I understand you, now. This could get costly in terms of
 resources, and I now better understand your interest in the libevent
 extension.

 I'd be tempted to write custom C extensions for a web server like nginx,
 which naturally handles asynchronous IO. And, in this case, you could even
 make use of fast polling instead of long polling, due to the performance of
 nginx, such as demonstrated in the this example:
 http://amix.dk/blog/post/19414


thanks for the advice,
but i don't know if it can be integrated into web pages.
where in web pages,
browsers will stop the requests to different ports even when the url having
the save domain name.

if i want to host more pages, add more interactions, it would be every
difficult.

so it would be better to have php solutions to make it easier :)



 And, don't worry about your English too much. My Chinese stops after ni hao
 ;)


ni hao is a good start:)


 Adam

 --
 Nephtali:  A simple, flexible, fast, and security-focused PHP framework
 http://nephtaliproject.com