Re: php server push



gkrisz@xxxxxxxxxx wrote:
Hi, (i hope this is the right place to ask questions like below)

I am trying to use content type 'multipart/x-mixed-replace' to achive server pushing and I have the following piece of code, which works perfectly.

/* file.html */
function handleContent(event)
{
var result = event.target.responseXML;
}

var xrequest = new XMLHttpRequest();
xrequest.multipart = true;
xrequest.open("GET","file.php",true);
xrequest.onload = handleContent;
xrequest.send(null);

/* file.php */

<?php
header('Content-type: multipart/x-mixed-replace;boundary="rn9012"');
print "--rn9012\n";


/* With these prints i can generate an event on the browser side. For instance if I would like to wait for sg to happen on the server side i can
make an infinite loop and wait. when sg happens i just print the event.

*/
while (true) {
print "Content-type: application/xml\n\n";
print "<?xml version='1.0'?>\n";
print "<content>event1</content>\n";
print "--rn9012\n";
flush();ob_flush();
}


sleep(5);

/* I close the connection with this event. Closing tag: with 2 extra -- */
print "Content-type: application/xml\n\n";
print "<?xml version='1.0'?>\n";
print "<content>last event</content>\n";
print "--rn9012--\n";

?>

BUT I have to keep an infinite loop on the server side for each clients
just to be able to send and event (let's say in every 3rd hour)

Is there a way with which i can keep the connection without the infinite loop,
and send the event message to the client from a different php file?

/* file2.php */
<?php
/* this is what i wish */
send_event_toclient(clientid, eventmessage);
//clientid: id of the client that i had saved before
//i think this id should be sg socket where i can write the output to.

?>

I hope I cound explain my problem clearly
thank you in advance

Christian Guttmann


ahh an age old problem; in short you need something like "meteor" to handle this (a different http server); tbh the http protocol isn't cut out for this.

You may want to save some time and look into XMPP with PUB-SUB; it's lightweight permanent client server connection with server push (ignite realtime/jive software do a great XMPP server that's opensource ;)

Regards and sorry for the vagueness - there are tonnes of articles everywhere on the net documenting the problems; this is why everybody poll's all the time.

if you do want a strictly PHP resolution then look into making your own multiprocess daemon with socket stream server / client and the stream_ functions - rather complicated and not a "standard" way fo doing things, but fun :)

--
nathan ( nathan@xxxxxxxxxxx )
{
Senior Web Developer
php + java + flex + xmpp + xml + ecmascript
web development edinburgh | http://kraya.co.uk/
}
.



Relevant Pages

  • Re: HELP: send binary replies back and forth ???
    ... a client, not a server. ... simple php function to set the timeout), ... Issues in writing php server are: ... >> and communication then goes back and forth between the server and client ...
    (comp.lang.php)
  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)
  • Re: How do we get there from here?
    ... >> executing on the server as a pre-processor. ... If there are client side versions of PHP, ... > name implies a static text file, is it different from a PHP script? ...
    (comp.databases.pick)
  • This is going straight to the pool room
    ... or not the client has privilege to do what they're trying to do, ... The server environment is this: ... 3GL User action Routines that Tier3 will execute on your behalf during the ... Routine Name: USER_INIT ...
    (comp.os.vms)
  • [Full-Disclosure] R: Full-Disclosure Digest, Vol 3, Issue 42
    ... Full-Disclosure Digest, Vol 3, Issue 42 ... SD Server 4.0.70 Directory Traversal Bug ... Arkeia Network Backup Client Remote Access ...
    (Full-Disclosure)