So I’ve spent some more time today dicking around with Comet, specifically the implementation (which is an implementation of the Bayeux protocol). The Bayeux protocol in itself is pretty interesting (though the documentation is shit) though I have some issues with it.
Effectively, an implementation of the protocol has a single endpoint by which all requests are made. Each request is associated with a “channel” and specifies an operation to make on that channel. The following operations are –
-
subscribe to the channel — server will send any updates
-
unsubscribe — server will stop sending updates
-
publish — server will push the raw data to anyone subscribed to the channel
The first two are fairly straightforward, but the third is kind of hazy and probably varies widely by implementation. Obviously, there’s a couple of ways that the publish event can be handled –
- Push the data out to all subscribers without any processing.
- Have the server review/transform/store the data before broadcasting it.
- Don’t broadcast stuff — just send the response back to the publishing client.
I should note here that 3 is actually an option in the Bayeux specification — any channel matching /service/* is a special channel which doesn’t permit subscribers and just pushes responses back to the client. And Erlycouch implements that option.
Method 1 is the de-facto way Bayeux is supposed to work — the clients just send shit around and the servers don’t do anything. Naturally, this is absolutely retarded: you never want to have clients sending data to clients if you don’t have the computational resources to verify that data (and in JavaScript, sorry, you don’t). I’m not really sure what the fuck crack they were smoking when they devised the protocol.
The protocol doesn’t really provide a means to get to #2. From what I can glean from it, they expected the clients to listen on one channel and publish to another, then have “server clients” read and publish or god I don’t fucking know it’s retarded. Anyway, the guy who wrote Erlycomet probably thought it was retarded too because he layered his own protocol on top of the Bayeux specification specifically to make RPC requests.
Basically, if you send a message to Erlycomet which, in the JSON payload has the following fields –
-
id — opaque identifier which gets passed back to the client in the response
-
method — name of the Erlang function in the RPC module to call
-
params — list of parameters to pass to the RPC function
then it tries to call the function specified. If the call works (ie, doesn’t raise an exception due to invalid arguments or missing function name or whatever) it broadcasts the result of the RPC call. If it doesn’t, it just reverts back to #1 and broadcasts the raw message to anyone subscribed.
In my application, I don’t want people sending random unvalidated shit around, so I ripped that “if the RPC call fails send the raw message” bullshit out. Every message is an RPC call which then pushes the validated message to any subscribed clients (and stores the state persistently and shit).
Tune in next time when I describe the fucking pig disgusting JavaScript shit that I’m writing right now.
2 comments