Re: php server push

From: Date: Thu, 02 Oct 2008 10:53:43 +0000
Subject: Re: php server push
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-281358@lists.php.net to get a copy of this message
gkrisz@upcmail.hu 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@kraya.co.uk ) { Senior Web Developer php + java + flex + xmpp + xml + ecmascript web development edinburgh | http://kraya.co.uk/ }

« previous php.general (#281358) next »