This seems to be done by an extraordinarily smart guy - Neil
Fraser. He's behind "google-diff-match-patch" [1] project,
which is AFAIK the machinery behind realtime collaboration
on google docs.
Definitely worth watching.
[1] http://code.google.com/p/google-diff-match-patch/