Sunday, February 26, 2012

I'll be posting an incremental StrifeBarge update later this week, but I wanted to think aloud about a particular piece of it first. My last article concluded with a paragraph which included the action-item

figure out a good way to periodically notify clients about new developments in the game

Don't worry if you missed it, that post may just have shatter the "most words" record for Langnostic, so it's perfectly understandable if you just read up the point where I begin talking about the code and then went to do something else. I quote it above because that quote just about sets the stage for this.

It turns out that periodic notifications from the server are still a pain in the ass. Your options, as of almost-March 2012 are Websockets, Comet (aka Long Poll, aka (God help me) "Reverse Ajax"), Ajax Polling and HTML5 Server-Sent-Events.

Websockets

seem to have gotten the most press, but have so far failed to materialize. If you're familiar with network programming[1], this will be intuitive to you. Instead of relying on the standard request/response architecture of the web, a client and server do a handshake and establish something like a traditional, secure, bi-directional socket through which they can send each other data. In theory, that completely eliminates the need for any of the other methods. In practice, I'm not convinced.

The big problem is that the word "secure" in the previous sentence should have been in quotes, because that's been a big point of contention among implementations. Websockets seem to be, right now, where JavaScript was circa 2002. The various clients are all doing crazy or semi-crazy things their own way, which means that a server either has to make peace with the fact that a large number of visitors won't be using the tool correctly, or it has to try to disambiguate between individual versions of various browsers and provide a tailored protocol for most of them. Fun times, huh?

The first place I heard of this concept was way back when Joe Armstrong posted a[2] Websocket example using YAWS, outright proclaiming the death of all the other options. I'm going to have to respectfully disagree, three years later. Of the big language implementations out there right now, only node.js has a decent solution for Websocket use. Essentially, they have a server and client framework that simplifies the interface, and provides automatic fallback behavior for clients that speak an older version of the protocol, or don't speak it at all, or speak it but disable it by default. Worryingly, the ultimate fall-through is a Flash applet that establishes that same socket connection, which means some people won't be getting this either, but at least it works most of the time. No one else seems to have thought it out quite as far[3].

In any case, this is a decent choice where you need true bi-directional communication, but it seems like implementing it here would cause me some unnecessary headaches, and I don't think turn-based games strictly require it.

Comet

This is just a bending of the standard request/response protocol that the web is built out of. Usually, the client sends a request and the server responds to it right away, either with the requested information or an error code. "Comet" is a name for the situation where the server instead sits on the request until there's something new to send over, at which point it responds and the client immediately sends a new request that the server will respond to at its leisure. That's actually a pretty good option, except that I happen to be using a server[4] that spawns a new thread per connection. In a Comet situation, this gets out of hand, because you essentially have a thread running constantly per user[5]. If I were running a single threaded server, this may be a better option, but as it stands, it seems like I'd have to do a lot more work for what I was hoping would be a simple project. So, no dice here either, sadly.

Ajax Polling

I'm reasonably sure everyone who cares knows what this is by now. You have a specific page built to send out updates, and each client has code that looks something like

The end result being that you can fake bi-directional communication by just having the client poke the server repeatedly and integrate data from the responses as it goes. The only issue with this approach is the overhead; go ahead and take a look at this breakdown of the process. Calling complexity aside[6], by my count, a request ends up transferring twice and a bit the obvious amount of data involved[7]. Some issues also arise from naive use of the method, which I'll get into with the final option I considered.

Server-Sent-Events

are basically formalized, lightweight Ajax polling with a few small benefits. The bad part is that you're still basically instructing the client to poke the server at a given interval, but the response is structured differently. Something like

data: Foo bar baz

with options, instead of the giant XML response. The options include multi-lining the message[8]

data: Foo bar
data: baz

providing each message with an identifier for synchronization purposes

The id message is automatically used by the client to sync messages[9], the event message can be used to set up different client behavior based on what kind of event happened on the server, and the retry message gives the server a way to tap out if too many users are pile-driving it at the moment. It still doesn't "solve" the fundamental asymmetry between client and server in HTTP, and it will never be as responsive as an actual socket connection, but it seems to be a Good Enough™ solution that address most of the issues I'd be thinking about if I tried to implement StrifeBarge using Ajax polling.

In addition to working on StrifeBarge for the next little while, I'll also be poking semi-seriously at node.js[10], so I may end up using websockets for something, but SSE wins it for the time being.

2 comments:

The main thing that scared me off of doing web development with common lisp was that there's no asynchronous web server. I can't see how a multithreaded web server will ever be able to handle a lot of users with push based communication. Either you block a whole thread, or you do polling, each of which is expensive.

Probably the best thing is to write at least part of your application in node js. It's become popular for doing push based stuff for a reason.

There is Antiweb, if you absolutely must have an async server. To be fair though, polling with SSEs doesn't seem like it would be a huge deal until you got to quite a few concurrent users (although I haven't tested this assumption in any way).

I think I mentioned node prominently enough; they're really the only ones I've found that have an acceptable implementation of websockets put together. I'll definitely be using it for some serious web development in the near future.

Ruby and Erlang each come with their own modes, and recent Emacs versions ship with a built-in Python mode and shell. Smalltalk uses its own environment (though GNU Smalltalk does have its own mode), and I'd really rather not talk about PHP. If you're writing in it, chances are you're using Eclipse or an IDE anyway.