ryah ([info]four) wrote,
@ 2007-05-08 15:41:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:projects, upload progress bar

Ajax.Pull
ajax_pull-0.1.zip

This is my Ajax stream pulling code. It's very simple: you make a XMLHttpRequest call to a server which streams a list of JSON objects separated by semi-colons. You give the library a handler function and this function gets called with each new JSON object that comes from the server.

The idea is that you can build things like upload progress bars without polling the server for each update.

If browsers followed the specification, this would work. I can assure you it won't work in Safari (419) due to a bug but I'm encouraged by [info]natan's hopeful prediction that with OSX 10.5, Apple will release Safari bug fixes to all users. Probably doesn't work in IE either. But maybe someone will still find it interesting, like, theoretically.

To try it

  1. run ruby test_mongrel.rb to start a test web server on port 4711.
  2. Navigate to http://localhost:4711/files/test.html. Do you see an ASCII face and numbers updating once per second?

Writing javascript/prototype is hilarious. Monstrous statements like this are considered clean (I think)
slice.extractJSON().each((function(statement) {
  this.handler(eval( '(' + statement + ')' ));
  this.pullPointer += statement.length + 1;
}).bind(this)); 



(5 comments) - (Post a new comment)


[info]evan
2007-05-08 03:52 pm UTC (link)
My first thought on reading your post was "how do you know where a literal ends". Then I looked at your code and saw that handling that was most of your code. Ugly! ;)

You could instead send an integer which is the number of bytes in the JSON literal that's following. So you'd grab /^(\d+) / off the head of the string, parse that, then grab the next $1 bytes for eval.

(Reply to this) (Thread)


[info]four
2007-05-08 10:27 pm UTC (link)
yeah-it's annoying. i wish i could do it with regular expressions.
your suggestion: but then you'd be sending invalid js and it makes writing server code annoying. i could force 'them' to put their JSON in a wrapper object
[size, real_data]
... shrug :)

(Reply to this) (Parent)


[info]natan
2007-05-09 03:55 am UTC (link)
Did you find a bug for this issue on WebKit Bugzilla?

(Reply to this) (Thread)


[info]four
2007-05-09 06:50 am UTC (link)
no, but the problem is not in the nightly build.

(Reply to this) (Parent)

A more elegant "half solution"
[info]orbanbotond
2008-02-13 04:46 pm UTC (link)
Hi Ry,

I tested this library with Firefox, Opera, IE6, IE7, Safari Webkit, I also tested your previous mongrel plugin.
It seems that the solution which works on almost any browser is the previous mongrel plugin which sends tags.
I found an much more elegant solution (at least the code is prety) which uses the "onInteractive" callback. Unfortunately doesn't works on a lot of browsers.

I have made a post about it:
http://orbanbotond.blogspot.com/

(Reply to this)


(5 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…