Thanks for the encouragement :-)
> seems to belong to some kind of relatively complex infrastructure that knows how to talk like this,
Hrm, thats fair comment. MochiKit has that lovely "just exactly what
you need. And no more" feel to it. I can see that dispatch.js or
something like that might well be a layer too far.
The client-side bit needs finishing. I started looking at
implementations of Protocol using HttpRequest but thought it would be
good to get some feedback/advice before I got too carried away. I have
that "some one must have done this before, and better" feeling. If
there isn't something out there that doesn't leave me thinking "hey I
just came in for a stake sandwidch and now you say I have to eat a
cow!" then I think I'll try to take dispatch.js further.
The basic idea is roughly:
Wrap up an HttpRequest such that it can be conveniently invoked at
regular intervals. (That's what Protocol is for)
Each time the request is triggered it gets two query parameters: The
first is all the 'event' messages generated by the client side, and
the second is all the responses the client has made for events
generated on the server side.
In the client-side, the responses to backend originated events, and
new client-side events, pile up in Dispatch until 'interval' seconds
after the last request completes. Both the client-side and the server
side are allowed to take more than one http request/response cycle to
redeem a request. Each side of the circuit (client or backend) is
responsible for numbering the event messages it originates.
event messages look like either of:
{'tag_query':id,'body':opaquedata}
{'tag_resonse':id,'body':opaquedata}
dispatcher.js `Dispatcher.query` is passed `opaquedata`. Dispatcher
generates an id and queues a 'tag_query' formed message for the next
request cycle. `Dispatcher.query` is how the client-side creates a new
event targeted at the backend. It takes an optional deferred, and
returns a new one if none is given. The returned deferred will be
fired later when the backend responds with a 'tag_response' whose id
matches the original query. The 'result' is the opaquedata from the
body of the tag response.
`Dispatcher.respond` takes whatever id the backend chose to give the
event message, and the opaquedata, as parameters for the reply to the
backend originated event. But whatever `id` is, it should to be
coercable, losslesly, to a js property name. The line begining with
`log("backend query` in Dispatch.dispatch_result is where the hooks
need to go to allow the rest of the client program to intercept
backend originated queries, Dispatch.respond is how the client should
reply when its ready.
So the client side js realy just cares about deferreds and the format
of opaquedata. The message 'container' format and numbering is delt
with by the combination of Dispatch & Protocol. They just add enough
wraping to the 'opaque' application event data in order for the
backend and dispatch.js to arrange for transport.
It is infact only Protocol that needs to care about 'tag_query' and
'tag_response', altho a little extra indirection in
Dispatch.dispatch_result is needed to fulfill this property. And only
Dispatch cares about the numbering/id scheme (but the backend has to
honour this). Imposition on the backend is then:
* manage tagging of events originated at the backend.
* honour tagging of events originated at the client (tag_response =
tag_query).
* Respond in an appropriately formed way to http/GET or http/POST
Event messages are allways in json format. Arguably this need not be
the case and the desicion rests with Protocol.
The desired end, is a bi-directional circuit ,pigy backing on a
polling http/s request, that delivers events sourced at either the
client or the backend.
I guess the question would be, is this sort of thing usefull enough,
and often enough to find a home in MochiKit, even in the examples ?
I think I'll start out by rustling up a couple of minimal backends.
Twisted I can do, and I've been itching for an excuse to fiddle with
TurboGears and others. I'll stick to a directory layout that makes
droping into mochikit/examples easy and see how things look after
another weekends worth of fiddling. Advice, Admonishments and stake
sandwiches ;-) greatfully accepted!
Cheers,
Robin
ps "must remember to reply to list", oops