Hixie's Natural Log

2004-04-28 15:45 UTC Server-sent DOM events

Someone suggested to me recently that it would be quite cool if there was a way in which servers could dispatch DOM events straight into a Web page so that script in the page could then react to them, updating the page and so forth.

At the moment pages fake this by opening an iframe to a page that the server keeps open, slowly trickling script blocks into it, and having the browser execute them as it finds them.

This is pretty ugly, but it works. So here is a proposal for a cleaner, declarative way to do this.

To specify an event source in an HTML document authors use a new (empty) element event-source, with an attribute src="" that takes a URI to open as a stream and, if the data found at that URI is of the appropriate type, treat as an event source.

The event stream MIME type is application/x-dom-event-stream.

The event stream is always be encoded as UTF-8. Line are always be terminated by a single U+000A line feed character.

The event stream format is (in pseudo-BNF):

<stream>  ::= <event>*
<event>   ::= [ <comment> | <field> ]+ <newline>
<comment> ::= ';' <data> <newline>
<field>   ::= <name> [ ':' <space>? <data> ]? <newline>
<name>    ::= one or more UNICODE characters other than ':' and U+000A
<data>    ::= zero or more UNICODE characters other than U+000A
<space>   ::= a single U+0020 character (' ')
<newline> ::= a single U+000A character

The stream is parsed by reading everything line by line, in blocks separated by blank lines (blank lines are those consisting of just a single lone line feed character). Comment lines (those starting with the character ';' not proceeded by any other characters) are ignored.

For each non-blank, non-comment line, the field name is first taken. This is everything on the line up to but not including the first colon (':') or the line feed, whichever comes first. Then, if there was a colon, the data for that line is taken. This is everything after the colon, ignoring a single space after the colon if there is one, up to the end of the line. If there was no colon the data is the empty string.


Field name: Field data
This is a blank field
1. These two lines: have the same data
2. These two lines:have the same data
1. But these two lines:  do not
2. But these two lines: do not

If a field name occurs multiple times, the data values for those lines are concatenated with a newline between them.

For example, the following:

Test: Line 1
Foo:  Bar
Test: Line 2

...is treated as having two fields, one called Test with the value Line 1\nLine 2 (where \n represents a newline), and one called Foo with the value Bar.

(Since any random stream of characters matches the above format, there is no need to define any error handling.)

Once the fields have been parsed, they are interpreted as follows (these are case sensitive exact comparisons):

Once a blank line is reached, an event of the appropriate type is synthesized and dispatched to the appropriate node as described by the fields above. No event is dispatched until a blank line has been received.

The RemoteEvent interface is defined as follows:

interface RemoteEvent : Event {
  readonly attribute DOMString       data;
  void               initRemoteEvent(in DOMString typeArg,
                                     in boolean canBubbleArg,
                                     in boolean cancelableArg,
                                     in DOMString dataArg);
  void               initRemoteEventNS(in DOMString namespaceURI,
                                       in DOMString type,
                                       in boolean canBubbleArg,
                                       in boolean cancelableArg,
                                       in DOMString dataArg);

Events that use the RemoteEvent interface never have any default action associated with them.

The event-source element may also have an onevent="" attribute. If present, the attribute is treated as script representing an event handler registered as non-capture listener of events with name event and the namespace uuid:755e2d2d-a836-4539-83f4-16b51156341f or null, that are targetted at or bubble through the element.

The following event description, once followed by a blank line:

Event: stock change
data: YHOO
data: -2
data: 10

...would cause an event stock change with the interface RemoteEvent to be dispatched on the event-source element, which would then bubble up the DOM, and whose data attribute would contain the string YHOO\n-2\n10 (where \n again represents a newline).

This could be used as follows:

<event-source src="http://stocks.example.com/ticker.php" id="stock">
<script type="text/javascript">
document.getElementById('stock').addEventListener('stock change',
  function () {
    var data = event.data.split(' ');
    updateStocks(data[0], data[1], data[2]);
  }, false);

...where updateStocks is a function defined as:

function updateStocks(symbol, delta, value) { ... }

...or some such.

What do you think? Let me know.

Pingbacks: 1

2004-04-12 19:27 UTC Embedding flash without <embed>

I've never written any Flash content, or, as far as I can recall, ever used Flash on any of my sites, and I don't even have the Flash plug-in installed here, since Flash is a proprietary language and thus inherently evil and should never be sent over the wire, but, someone asked how to get around the fact that embed is not a valid element in HTML4 Strict, given that tutorials always encourage people to use one object element for IE and then a nested embed element for everything else, so I wrote a sample page to do just that.

Pingbacks: 1 2 3 4 5

2004-04-11 21:12 UTC Sunny newspaper clipping

I was reading Google News earlier today...

... "See loads more girls on our super cyber site" The Sun, 7 hours ago. "A white house memo released today has revealed that US President George Bush was warned about a possible hijacking plot inside America more than a month before September 11 2001." ... "The selection and placement of stories on this page were determined automatically by a computer program."

No comment.

Pingbacks: 1

2004-04-06 16:41 UTC Ow

Today we went skiing.

Tomorrow we will be actively not doing anything.

2004-03-28 20:33 UTC Ramblings from the North

I'm at the back of a dark room sitting next to an air vent listening to Let Me Entertain You being played by a big band on a stage 20m in front of me.

Look me up in the yellow pages
I will be your rock of ages
Your see-through fads and your crazy phases, yeah
Little Bo Peep has lost his sheep...

This dark room is far underground for a shopping mall which has been rented away for the night for the purposes of entertaining 4000 people. Actually the mall is only half the party, they are also using the concert hall across the street.

The 4000 people are all musicians, members of Norwegian concert bands who are here for the annual concert band competition. I'm here because I'm a member of one of those bands, the Romerike Konsertorkester.

"Here" is in Trondheim, the furthest north I've ever been as far as I know. This town has the prettiness of Bergen but the weather of Oslo. In fact the weather is probably even worse than Oslo. It was sunny yesterday (if ridiculously cold), but today someone aimed an incessant stream of crystallized water pellets at the town for no apparent reason, and it became all slippery, white, and wet.

The band I'm in came fourth out of about twelve in our division. We're in division 1. (It's a zero-based index. There is one division above us, the Elite division, and three divisions below us.) I've never been here before so I really have no point of reference, but based on the looks of extreme joy on my fellow band members, I'm guessing this is extremely good. (We were apparently hoping to be lucky and place eighth or ninth.)

I'm currently singing along quite loudly to the music being played on stage. It's drawing looks. It has to be said, I'm the only person who has a laptop here. They are all enjoying themselves with alcohol and tobacco, much to my disgust, but hey, to each his own.

Before sitting down here I tried to get myself a drink.

Me: Har du eplemost?

Bartender: ...?

Me: Har du eplemost?

Bartender: ...!


Bartender: ...!!

They didn't have any apple juice.

Me: Vann?

Bartender: ...?

Me: Vann!

Bartender: ...?!

Me: VANN!!

Bartender: ...!!

They didn't have any water either.

Eventually I found a bar that did have water, about 100m from where I'm sitting now. I came back to enjoy the Jazz with my glass; by the time I'd gotten here I'd drunk half of it already.

I'm not going for more water for a while. Trying to push myself through the increasingly drunk people isn't too fun.

The age range here is quite dramatic. There are people from early teens all the way to retirement age. Sadly the smoker population spans the same age range. There are also quite a few cute girls, although many are smokers too, and none of them are the right cute girl.

Much to my amusement somebody just asked me if what I was doing (using a computer) had anything to do with what was going on around me. I went for "no". How anyone can communicate with the level of noise in here I have no idea. There's no wireless network, so the obvious answer (an IRC network) is right out. It certainly isn't vocally. I can't hear a word of what people say even when they shout in my ear. I've often wondered this about events of this nature.

As is probably obvious, I don't go to these kinds of these parties very often. My kind of party is usually very quiet, with very little sound except the hum of several dozen fans and the occasional expletive from an indignant party-goer. (And at that kind of party, IRC usually is indeed the preferred communication method.)

Young man, there's no need to feel down.
I said, young man, pick yourself off the ground.
I said, young man, 'cause you're in a new town,
there's no need to be unhappy.

Everybody just stuck their arms in the air simultaneously, then folded them, then threw them to the side, then brought them together again above their head. How odd.

The same guy just asked me what I was doing again. I gave him the same answer (DIARY!!) and he nodded and went away again. He'll probably be back.

In other news, I recently bought 30 DVDs from the UK. (Not coincidentally, last weekend my parents came to visit, which was nice, since I hadn't seen them in ages.) I would have bought the DVDs in Norway, but the prices here are astronomical, and despite this being highly appropriate given the content of the DVDs, it was not very attractive to me.

This purchase increased my total DVD ownership by a good base-10 order of magnitude. And it is quite impressive on the shelf.

The DVDs were seasons one through six of Stargate SG-1. My flatmates are eating it up like addicts, it's quite funny. They watched season one in about a week. They'll probably have finished season two by the time I get back tomorrow. I'm having a great time rewatching the early episodes, too... I might even resume my detailed analysis of key episodes which I started a year or so ago.

Jack O'Neill: ...what now?

Master Bra'tac: Now we die.

Jack O'Neill: Well that's a bad plan.

My ulterior motive for purchasing these DVDs was actually to attempt to get someone hooked on SG-1. I'm going to get an extended period of quality time with this someone and her three cute ferrets at Easter to attempt this.

I didn't used to think ferrets were cute, by the way. Now I consider them almost honorary cats. That's quite a statement, coming from me.

During the rehearsals for the band I mentioned above we sometimes spend many minutes playing sections where I have long 24 bar rests, so I've been taking the opportunity to work on a proposal for a Web Applications specification. This would be something along the same lines as Web Forms 2, but specifically for client-side application development. (I asked for ideas along these lines a few weeks ago — thanks again to those of you who responded.)

Some of the things I'm thinking of are:

The house lights just went up for a split second (someone probably leant on the wrong switch) and after they went back down, a girl came over and asked me what I was doing. I'm writing about the party, I said. She found this fascinating. I have a feeling "I'm brainstorming ideas for extensions to HTML to enable richer user experiences with Web applications" would have gotten a rather less warm response, but maybe I should try it if someone asks again. Sadly they would probably reply "but you should come enjoy yourself! Have a beer! Horrah! Skål!" or some such, oblivious to the fact that I live for this spec stuff and don't like the taste of beer... Did I mention "to each his own" already?

Speaking of girls, I've noticed I get a lot more looks from girls now that I've shaved and cut my hair. I don't recall ever noticing this happen when I cut my hair in the past. I wonder what the difference is this time.

Incidentally, XBL is progressing nicely, despite seemingly insurmountable differences between "us" (the CSS working group members who are interested in XBL) and "them" (the SVG working group members who were behind RCC). I have a bunch of action items to deal with tomorrow, and then we'll be down to pretty much only one real blocking issue before publication.

Pingbacks: 1