Ben's blog

ramblings from cyberspace

So, because I've been thinking a lot about how to do small projects on the web without all the complexity inherent in modern stacks I came upon an idea that might be able to allow for efficient WebSockets with a (Fast)CGI backend.

It seems that most ways allowing for WebSockets really really want you to have a HTTP server running your backend code all the time. This really irks me because I absolutely love how with CGI only your HTTP server needs to run all the time and enforces each request to be dealt with in complete isolation. This allows for easy experimentation/updates since if you break something the HTTP server will just return a 500 page and the next request might just succeed, this makes an application quite resilient since I've seen it far too often that one bad request brings down the whole backend because of some uncaught exception.

This also allows for much better GC schemes, mainly just don't collect at all since you can just free the entire heap at the end.

But enough praise of the CGI model, since I've wanted to preserve these characteristics but still allow for efficient server initiated updates I needed something to bring WebSockets into the CGI world.

The idea is to have a simple Rust program act as a bridge, in that it translates each WebSocket event into a separate HTTP request. To send a message one can just POST to that same Rust program. That's it. That way the only programs that need to be running all the time are the HTTP server and the Rust bridge, both programs that have a very specific feature set and don't need to change according to application needs.

And while doing a separate HTTP request for every WebSocket event might sound inefficient, with most projects there's just not that much going on, and since even somewhat slow languages can easily serve more than 1k req/s this shouldn't be a problem (since only an event triggers a request, a WebSocket that's just open should be pretty inexpensive).

So, if you're interested you can see the current state over on GitHub. Today I've mostly set things up and started on a very simple PoC PHP Chatroom, once things stabilize I'll see what else can be built with it, and I'm also itching to get Nujel hooked up to it, would be pretty cool to develop fancy WebApps in my own language.