Integrating Mobwrite with nginx

I recently helped an old friend with some prototyping and strategy for a stealth-mode project he’s doing. He wanted to use MobWrite, a framework that allows multiple users to do real-time, collaborative editing of content in a web page.

There was one technical challenge: the project was using nginx as a webserver, and none of the existing web gateway implementations for MobWrite were well-suited for use with nginx. MobWrite has a back-end server that speaks a custom protocol, and it needs a bit of additional software to translate HTTP requests from web browsers into the server’s protocol. The MobWrite distribution contains PHP, JSP, and Python pages that do this translation.

It is possible to add support for any and all of those languages to nginx, but doing so adds substantial complexity. Specifically, the event-driven design of nginx works well only with plugins that do their work in small, event-driven steps and do not block for I/O or lengthy computation. PHP, mod_python, and JSP, in contrast, all are based on a programming model where the code that handles the request expects to run synchronously until completion. Running such code inside the single-threaded, asynchronous nginx could allow a long-running PHP/Python/JSP request to block all other requests. One common solution is to use an nginx gateway plugin like mod_scgi that dispatches requests for these synchronous languages to a separate pool of processes. This enables a single nginx server to handle both synchronous PHP/Python/JSP requests and asynchronous static-content or HTTP-proxy requests without either type of request starving out the other. However, adding a separate process pool for the synchronous requests means adding configuration and tuning complexity, thus diminishing one of the inherent advantages of using nginx.

Determined to have my cake and eat it too–i.e., to support MobWrite and other types of content in a single nginx server while still enjoying nginx’s single-process simplicity and efficiency–I made a new nginx plugin called nginx-mobwrite. This plugin translates incoming HTTP requests from MobWrite clients into the MobWrite internal format and proxies them asynchronously to the MobWrite server. The nginx architecture includes an “upstream” framework that manages requests to back end servers as part of nginx’s event loop. The nginx-mobwrite uses this framework to dispatch translated requests to the MobWrite server and send the responses back to clients in an asynchronous manner, without blocking any other requests the nginx server might be handling at the same time.

The nginx-mobwrite plugin is available as open source, under v2.0 of the Apache License, on GitHub.

You must be logged in to post a comment. Don't worry; you needn't register for a new account here. You can log in securely via your Google/Gmail, Yahoo, Facebook, or OpenID account.