I'm writing a javascript / PHP library for handling 'push' streaming to the web browser. It can be used for stuff like a REALLY smooth web irc client or MSN client or live chat client..etc..
The javascript opens one constant connection to the PHP and never closes it, the PHP then pushes json objects down to the javascript when it needs to.
It's been a bit of a nightmare so far.. I've made one 'Stream' object which can take many forms depending on which browser you're using.
If you're in firefox it'll use XMLHttpRequest, If you're in internet explorer it uses "htmlpage" activeX object, then it'll fall back to a 'flash' proxy if flash is installed, and finally if all else fails it'll use a hidden iframe. (might even do a silverlight version if i can).
So, it works! Hurrah. I'm logged into IRC via the web browser and it keeps me permanently logged in, it's completely asyncronious, I recieve new messages instantly and it's very very simple to implement.. for example:
javascript code:
var myStream = new Stream();
myStream.onRecieveCommand = function (json){
switch(json.type){
case "RECIEVE_MESSAGE":
this.onReceiveMessage(json.params["user"], json.params["time"], json.params["message"]);
break;
default:
alert('no implementation');
}
}
myStream.open("irc.php);
</span
php code:
public class MessageStreamCommand extends StreamCommand {
private $type = "RECIEVE_MESSAGE";
function __construct ($user, $time, $message){
$this->setUser($user);
$this->setTime($time);
$this->setMessage($message);
}
}
$stream = new Stream();
while (//reading from irc server) {
if (//recieved a new mesasge){
$stream->broadcast(new MessageStreamCommand($name, $time, $message));
}
}
This all works nicely. Quite proud of it, infact! The javascript involved in getting the actual 'streaming' work is a pretty sexy bit of work.
Now, my problem. I'm trying to work out how to send messages in the other direction (from the javascript to the PHP to the IRC or whatever).
One method I thought of was to open a second ajax connection for outgoing messages and those messages get stored in a database, and my main streaming script checks the database periodically for any messages that it needs to push.
Unfortunately with PHP being single threaded, I'm not sure how to achieve this. Anyone got any ideas???
In .NET this'd be nice and easy.. but I really want to start with a PHP solution first.EDITED: 31 Dec 2008 15:18 by MIKEE