[{"data":1,"prerenderedAt":84},["ShallowReactive",2],{"\u002Ftlt-2006.10.19-ajax-long-pull-(almost-http-push)":3},{"id":4,"title":5,"body":6,"createdAt":71,"description":12,"draft":72,"extension":73,"image":74,"mastodonThread":74,"meta":75,"navigation":76,"path":77,"seo":78,"stem":79,"tags":80,"updatedAt":82,"__hash__":83},"content\u002FTLT - 2006.10.19 - AJAX Long-Pull (almost HTTP Push).md","TLT_-_2006.10.19_-_AJAX_Long-Pull_(almost_HTTP_Push)",{"type":7,"value":8,"toc":68},"minimark",[9,13,30,39,42,53,56,65],[10,11,12],"p",{},"Tags: Perl, Continuity, AJAX, HTTP Push, Code",[10,14,15,16,23,24,29],{},"I've been doing some fun things with ",[17,18,22],"a",{"href":19,"rel":20},"http:\u002F\u002Fcontinuity.tlt42.org\u002F",[21],"nofollow","Continuity"," in conjunction with scott to prepare for ",[17,25,28],{"href":26,"rel":27},"http:\u002F\u002Fdesertcodecamp.org\u002F",[21],"Desert Code Camp",". The latest is combining XmlHttpRequest with Continuity in a new way, simulating HTTP Push.",[10,31,32,33,38],{},"It's really easy, actually. I put together a chat application which you can see in the ",[17,34,37],{"href":35,"rel":36},"http:\u002F\u002Fthelackthereof.org\u002Fprojects\u002Fperl\u002FContinuity\u002Feg\u002F",[21],"Continuity Examples Directory",". When you first go to the page, it immediately sends a background GET request back to the server, which actually opens a second session. That session then blocks, waiting for a variable to change to indicate that there are new messages.",[10,40,41],{},"So I have this code which watches for new chat messages:",[43,44,49],"pre",{"className":45,"code":47,"language":48},[46],"language-text","sub pushstream {\n  my ($req) = @_;\n  while(1) {\n    print STDERR \"**** Waiting for message ****\\n\";\n    my $w = Coro::Event->var(var => \\$got_message, poll => 'w');\n    $w->next->cancel; # Block, waiting for an update\n    print STDERR \"**** GOT MESSAGE, SENDING ****\\n\";\n    my $log = join \"\u003Cbr>\", @messages;\n    $req->print($log);\n    $req->next; # Once the client is ready, they'll ask for more\n  }       \n}       \n","text",[50,51,47],"code",{"__ignoreMap":52},"",[10,54,55],{},"Then, when the mainloop gets a new message, it just sets $got_message = 1 and all the watchers are triggered, giving all the clients the new message.",[10,57,58,59,64],{},"For fun, I opened up four webbrowsers (FF, IE6, Opera, and Konqueror) and recorded a chat between all four -- ",[17,60,63],{"href":61,"rel":62},"http:\u002F\u002Fthelackthereof.org\u002Ftmp\u002Fs1.html",[21],"Four Browser AJAX Long-Pull (fake Push) Demo",".",[10,66,67],{},"This is not really very robust -- but it should only take a few properly placed timeouts to fix that. I hear proxies can wreak havoc on this method too, so keep that in mind.",{"title":52,"searchDepth":69,"depth":69,"links":70},2,[],"2006-10-20T03:07-04:00",false,"md",null,{},true,"\u002Ftlt-2006.10.19-ajax-long-pull-(almost-http-push)",{"title":5,"description":12},"TLT - 2006.10.19 - AJAX Long-Pull (almost HTTP Push)",[81],"blog","2006-11-19T23:08-05:00","EDQjdl_FANIj_bw-80C4pGY0W74_w2niTFJoPjpvnJk",1789166548321]