Elnode

Elnode is an asynchronous HttpServer framework. You can use it to write HTTP services in EmacsLisp.

http://nic.ferrier.me.uk/stuff/img//elnode-access-log-screenshot.png Elnode access log buffer

What Elnode is

Elnode is a non-blocking IO webserver written entirely in EmacsLisp. It’s like an EmacsLisp version of node.js.

Elnode’s performance is better than I expected, we can get about 5000 request per second out of it. The limiting factor is likely Emacs async IO which uses the select function. Replacing that with epoll or some other fast equivalent would likely give us node.js like performance.

Elnode comes with a webserver and a wiki server application.

What is Elnode good for?

Elnode has been optimized for rapid development. It’s designed to get you going quickly. Emacs is a great development environment for EmacsLisp and that’s why Elnode applications are rapid and easy to build.

Some suggestions:

What is node.js?

node.js is an evented IO server framework in Javascript. node.js is not a browser thing. It is a server side thing. You write event driven servers in it. That means you write all your code in a single process and rely on the natural blocking of clients to give you concurrency. Of course, a standard technique is to to spawn processes to handle work - again, communicating asynchronously.

Using the builtin Wiki

Installing the latest versions of Elnode should automatically install working Wiki and Webserver apps listening on port 8000.

The webserver, by default, serves files (with an example included) from: ~/.emacs.d/elnode/public_html. Lots of stuff about the builtin webserver (including where it serves files from) can be configured with Elnode customizations:

M-x customize-group [RET] elnode [RET]

to see them all.

The Wiki server, by default, serves Wiki pages from ~/.emacs.d/elnode/wiki. That can be configured too.

An example with Elnode

Want to publish a directory of org-mode files? Here’s a full Elnode program that defines a handler to publish org files in a directory and also starts a server on port 8002.

  ;;; server.el ---  -*- lexical-binding: t -*-
  (defvar my-org-dir "~/work/org")

  (defun my-elnode-org-handler (httpcon)
    (elnode-docroot-for my-org-dir
        with org-file
        on httpcon
        do (with-current-buffer (find-file-noselect org-file)
             (let ((org-html
                    ;; This might throw errors so you could condition-case it
                    (org-export-as-html 3 nil 'string)))
               (elnode-send-html httpcon org-html)))))

  (elnode-start 'my-elnode-org-handler :port 8002) 

Now you can visit one of those files directly. A bit more work would be adding an index so you could pick which org-file you wanted to look at.

More org examples

Lot’s of people ask for org-mode Elnode integration so NicFerrier started a specific GitHub project to package up some tools and utils.

Getting Elnode

The latest version of Elnode is 0.9.8.

The latest development version of Elnode is 0.9.9 and includes:

The 1.0.0 version is planned and will include:

Elnode and databases

Because database access is such an important part of web applications, Elnode includes a simple database layer. The builtin implementation serializes Lisp objects to files.

Other database implementations are being worked on, NicFerrier is working on a MongoDb implementation and on a PostGreSQL implementation.

Talking about Elnode

We talk about Elnode on #emacs, I am often there so just shout me. Other people on #emacs know quite a lot about Elnode too.

We don’t have a mailing list yet. If you would like to provide one (ad free please) then please get in touch.

A tutorial for Elnode

People asked for a tutorial, so NicFerrier has started to make one. Progress is here.

You need the latest version of Elnode to make the tutorial work.


CategoryExtensions