<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Simon Willison's Weblog: ryan-dahl</title><link href="http://simonwillison.net/" rel="alternate"/><link href="http://simonwillison.net/tags/ryan-dahl.atom" rel="self"/><id>http://simonwillison.net/</id><updated>2024-09-17T23:20:37+00:00</updated><author><name>Simon Willison</name></author><entry><title>Oracle, it’s time to free JavaScript.</title><link href="https://simonwillison.net/2024/Sep/17/oracle-its-time-to-free-javascript/#atom-tag" rel="alternate"/><published>2024-09-17T23:20:37+00:00</published><updated>2024-09-17T23:20:37+00:00</updated><id>https://simonwillison.net/2024/Sep/17/oracle-its-time-to-free-javascript/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://javascript.tm/"&gt;Oracle, it’s time to free JavaScript.&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Oracle have held the trademark on JavaScript since their acquisition of Sun Microsystems in 2009. They’ve continued to renew that trademark over the years despite having no major products that use the mark.&lt;/p&gt;
&lt;p&gt;Their December 2019 renewal included &lt;a href="https://tsdr.uspto.gov/documentviewer?caseId=sn75026640&amp;amp;docId=SPE20191227132243&amp;amp;linkId=2#docIndex=1&amp;amp;page=1"&gt;a screenshot of the Node.js homepage&lt;/a&gt; as a supporting specimen!&lt;/p&gt;
&lt;p&gt;Now a group lead by a team that includes Ryan Dahl and Brendan Eich is coordinating a legal challenge to have the USPTO treat the trademark as abandoned and “recognize it as a generic name for the world’s most popular programming language, which has multiple implementations across the industry.”

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="https://lobste.rs/s/jupy5r/oracle_it_s_time_free_javascript"&gt;Lobste.rs&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/brendan-eich"&gt;brendan-eich&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/oracle"&gt;oracle&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;&lt;/p&gt;



</summary><category term="brendan-eich"/><category term="javascript"/><category term="oracle"/><category term="ryan-dahl"/></entry><entry><title>What we got wrong about HTTP imports</title><link href="https://simonwillison.net/2024/Jul/30/what-we-got-wrong-about-http-imports/#atom-tag" rel="alternate"/><published>2024-07-30T06:33:11+00:00</published><updated>2024-07-30T06:33:11+00:00</updated><id>https://simonwillison.net/2024/Jul/30/what-we-got-wrong-about-http-imports/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://deno.com/blog/http-imports"&gt;What we got wrong about HTTP imports&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
HTTP imports are one of the most interesting design features of Deno:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts";
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Six years after their introduction, Ryan Dahl reviews their disadvantages:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Lengthy (non-memorable) URLs littering the codebase&lt;/li&gt;
&lt;li&gt;A slightly cumbersome &lt;code&gt;import { concat } from "../../deps.ts";&lt;/code&gt; pattern for managing dependencies in one place&lt;/li&gt;
&lt;li&gt;Large projects can end up using multiple slightly different versions of the same dependencies&lt;/li&gt;
&lt;li&gt;If a website becomes unavailable, new builds will fail (existing builds will continue to use their cached version)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Deno 2 - due in September - will continue to support them, but will lean much more on the combination of import maps (design borrowed from modern browsers) and the Deno project's &lt;a href="https://jsr.io/"&gt;JSR&lt;/a&gt; npm competitor. An import map like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;{
  "imports": {
    "@std/assert": "jsr:@std/assert@1"
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Will then enable import statements that look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import { assertEquals } from "@std/assert";
&lt;/code&gt;&lt;/pre&gt;

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="https://lobste.rs/s/vfnby3/what_we_got_wrong_about_http_imports"&gt;lobste.rs&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/deno"&gt;deno&lt;/a&gt;&lt;/p&gt;



</summary><category term="javascript"/><category term="ryan-dahl"/><category term="deno"/></entry><entry><title>Deno Deploy Beta 3</title><link href="https://simonwillison.net/2021/Nov/7/deno-deploy-beta-3/#atom-tag" rel="alternate"/><published>2021-11-07T01:51:30+00:00</published><updated>2021-11-07T01:51:30+00:00</updated><id>https://simonwillison.net/2021/Nov/7/deno-deploy-beta-3/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://deno.com/blog/deploy-beta3/"&gt;Deno Deploy Beta 3&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
I missed Deno Deploy when it first came out back in June: it’s a really interesting new hosting environment for scripts written in Deno, Node.js creator Ryan Dahl’s re-imagining of Node.js. Deno Deploy runs your code using v8 isolates running in 28 regions worldwide, with a clever BroadcastChannel mechanism (inspired by the browser API of the same name) that allows instances of the server-side code running in different regions to send each other messages. See the “via” link for my annotated version of a demo by Ondřej Žára that got me excited about what it can do.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="https://til.simonwillison.net/deno/annotated-deno-deploy-demo"&gt;My TILs&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/v8"&gt;v8&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/deno"&gt;deno&lt;/a&gt;&lt;/p&gt;



</summary><category term="javascript"/><category term="ryan-dahl"/><category term="v8"/><category term="deno"/></entry><entry><title>Deno 1.0</title><link href="https://simonwillison.net/2020/May/13/deno-10/#atom-tag" rel="alternate"/><published>2020-05-13T23:38:27+00:00</published><updated>2020-05-13T23:38:27+00:00</updated><id>https://simonwillison.net/2020/May/13/deno-10/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://deno.land/v1"&gt;Deno 1.0&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Deno is a new take on server-side JavaScript from a team lead by Ryan Dahl, who originally created Node.js. It’s built using Rust and crammed with fascinating ideas—like the ability to import code directly from a URL.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/nodejs"&gt;nodejs&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/rust"&gt;rust&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/deno"&gt;deno&lt;/a&gt;&lt;/p&gt;



</summary><category term="javascript"/><category term="nodejs"/><category term="ryan-dahl"/><category term="rust"/><category term="deno"/></entry><entry><title>GitHub Actions ci.yml for deno</title><link href="https://simonwillison.net/2019/Dec/18/github-actions-ciyml-deno/#atom-tag" rel="alternate"/><published>2019-12-18T08:49:40+00:00</published><updated>2019-12-18T08:49:40+00:00</updated><id>https://simonwillison.net/2019/Dec/18/github-actions-ciyml-deno/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/denoland/deno/blob/c93ae0b05a4c4fe5b43a9bd2b6430637b17979d0/.github/workflows/ci.yml"&gt;GitHub Actions ci.yml for deno&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Spotted this today: it’s one of the cleanest examples I’ve seen of a complex CI configuration for GitHub Actions, testing, linting, benchmarking and building Ryan Dahl’s Deno JavaScript runtime.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/continuous-integration"&gt;continuous-integration&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/github"&gt;github&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/github-actions"&gt;github-actions&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/deno"&gt;deno&lt;/a&gt;&lt;/p&gt;



</summary><category term="continuous-integration"/><category term="github"/><category term="ryan-dahl"/><category term="github-actions"/><category term="deno"/></entry><entry><title>Node.js is genuinely exciting</title><link href="https://simonwillison.net/2009/Nov/23/node/#atom-tag" rel="alternate"/><published>2009-11-23T12:50:22+00:00</published><updated>2009-11-23T12:50:22+00:00</updated><id>https://simonwillison.net/2009/Nov/23/node/#atom-tag</id><summary type="html">
    &lt;p&gt;I gave a talk on Friday at &lt;a href="http://2009.full-frontal.org/"&gt;Full Frontal&lt;/a&gt;, a new one day JavaScript conference in my home town of Brighton. I ended up throwing away my intended topic (JSONP, APIs and cross-domain security) three days before the event in favour of a technology which first crossed my radar &lt;a href="http://simonwillison.net/2009/Nov/9/node/"&gt;less than two weeks ago&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That technology is Ryan Dahl's &lt;a href="http://nodejs.org/"&gt;Node&lt;/a&gt;. It's the most exciting new project I've come across in quite a while.&lt;/p&gt;

&lt;p&gt;At first glance, Node looks like yet another take on the idea of server-side JavaScript, but it's a lot more interesting than that. It builds on JavaScript's excellent support for event-based programming and uses it to create something that truly plays to the strengths of the language.&lt;/p&gt;

&lt;p&gt;Node describes itself as "evented I/O for V8 javascript". It's a toolkit for writing extremely high performance non-blocking event driven network servers in JavaScript. Think similar to &lt;a href="http://twistedmatrix.com/"&gt;Twisted&lt;/a&gt; or &lt;a href="http://rubyeventmachine.com/"&gt;EventMachine&lt;/a&gt; but for JavaScript instead of Python or Ruby.&lt;/p&gt;

&lt;h4&gt;Evented I/O?&lt;/h4&gt;

&lt;p&gt;As I discussed in my talk, event driven servers are a powerful alternative to the threading / blocking mechanism used by most popular server-side programming frameworks. Typical frameworks can only handle a small number of requests simultaneously, dictated by the number of server threads or processes available. Long-running operations can tie up one of those threads - enough long running operations at once and the server runs out of available threads and becomes unresponsive. For large amounts of traffic, each request must be handled as quickly as possible to free the thread up to deal with the next in line.&lt;/p&gt;

&lt;p&gt;This makes certain functionality extremely difficult to support. Examples include handling large file uploads, combining resources from multiple backend web APIs (which themselves can take an unpredictable amount of time to respond) or providing comet functionality by holding open the connection until a new event becomes available.&lt;/p&gt;

&lt;p&gt;Event driven programming takes advantage of the fact that network servers spend most of their time waiting for I/O operations to complete. Operations against in-memory data are incredibly fast, but anything that involves talking to the filesystem or over a network inevitably involves waiting around for a response.&lt;/p&gt;

&lt;p&gt;With Twisted, EventMachine and Node, the solution lies in specifying I/O operations in conjunction with callbacks. A single event loop rapidly switches between a list of tasks, firing off I/O operations and then moving on to service the next request. When the I/O returns, execution of that particular request is picked up again.&lt;/p&gt;

&lt;p&gt;(In the talk, I attempted to illustrate this with a questionable metaphor involving &lt;a href="http://www.slideshare.net/simon/evented-io-based-web-servers-explained-using-bunnies"&gt;hamsters, bunnies and a hyperactive squid&lt;/a&gt;).&lt;/p&gt;

&lt;iframe src="https://www.slideshare.net/slideshow/embed_code/key/B8ICSKJbZ2cBHw?startSlide=1" width="597" height="486" frameborder="0"   marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px;   margin-bottom:5px;max-width: 100%;" allowfullscreen="allowfullscreen"&gt;
&lt;/iframe&gt;

&lt;h4&gt;What makes Node exciting?&lt;/h4&gt;

&lt;p&gt;If systems like this already exist, what's so exciting about Node? Quite a few things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;JavaScript is extremely well suited to programming with callbacks&lt;/strong&gt;. Its anonymous function syntax and closure support is perfect for defining inline callbacks, and client-side development in general uses event-based programming as a matter of course: run this function when the user clicks here / when the Ajax response returns / when the page loads. JavaScript programmers already understand how to build software in this way.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Node represents a clean slate&lt;/strong&gt;. Twisted and EventMachine are hampered by the existence of a large number of blocking libraries for their respective languages. Part of the difficulty in learning those technologies is understanding which Python or Ruby libraries you can use and which ones you have to avoid. Node creator Ryan Dahl has a stated aim for Node to never provide a blocking API - even filesystem access and DNS lookups are catered for with non-blocking callback based APIs. This makes it much, much harder to screw things up.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Node is small&lt;/strong&gt;. I read through the &lt;a href="http://nodejs.org/api.html"&gt;API documentation&lt;/a&gt; in around half an hour and felt like I had a pretty comprehensive idea of what Node does and how I would achieve things with it.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Node is fast&lt;/strong&gt;. V8 is the fast and keeps getting faster. Node's event loop uses Marc Lehmann's highly regarded &lt;a href="http://software.schmorp.de/pkg/libev.html"&gt;libev&lt;/a&gt; and &lt;a href="http://software.schmorp.de/pkg/libeio.html"&gt;libeio&lt;/a&gt; libraries. Ryan Dahl is himself something of a speed demon - he just replaced Node's HTTP parser implementation (already pretty speedy due to it's Ragel / Mongrel heritage) with a &lt;a href="http://four.livejournal.com/1033160.html"&gt;hand-tuned C implementation&lt;/a&gt; with some impressive characteristics.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Easy to get started&lt;/strong&gt;. Node ships with all of its dependencies, and compiles cleanly on Snow Leopard out of the box.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With both my JavaScript and server-side hats on, Node just feels right. The APIs make sense, it fits a clear niche and despite its youth (the project started in February) everything feels solid and well constructed. The rapidly growing community is further indication that Ryan is on to something great here.&lt;/p&gt;

&lt;h4&gt;What does Node look like?&lt;/h4&gt;

&lt;p&gt;Here's how to get Hello World running in Node in 7 easy steps:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;samp&gt;git clone git://github.com/ry/node.git&lt;/samp&gt; (or download and extract &lt;a href="http://github.com/ry/node/archives/master" title="Download ry/node from GitHub"&gt;a tarball&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;samp&gt;./configure&lt;/samp&gt;&lt;/li&gt;
  &lt;li&gt;&lt;samp&gt;make&lt;/samp&gt; (takes a while, it needs to compile V8 as well)&lt;/li&gt;
  &lt;li&gt;&lt;samp&gt;sudo make install&lt;/samp&gt;&lt;/li&gt;
  &lt;li&gt;Save the below code as &lt;samp&gt;helloworld.js&lt;/samp&gt;&lt;/li&gt;
  &lt;li&gt;&lt;samp&gt;node helloworld.js&lt;/samp&gt;&lt;/li&gt;
  &lt;li&gt;Visit &lt;samp&gt;http://localhost:8080/&lt;/samp&gt; in your browser&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's helloworld.js:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;var sys = require('sys'), 
  http = require('http');

http.createServer(function(req, res) {
  res.sendHeader(200, {'Content-Type': 'text/html'});
  res.sendBody('&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;');
  res.finish();
}).listen(8080);

sys.puts('Server running at http://127.0.0.1:8080/');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you have Apache Bench installed, try running &lt;samp&gt;ab -n 1000 -c 100 'http://127.0.0.1:8080/'&lt;/samp&gt; to test it with 1000 requests using 100 concurrent connections. On my MacBook Pro I get 3374 requests a second.&lt;/p&gt;

&lt;p&gt;So Node is fast - but where it really shines is concurrency with long running requests. Alter the helloworld.js server definition to look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;http.createServer(function(req, res) {
  setTimeout(function() {
    res.sendHeader(200, {'Content-Type': 'text/html'});
    res.sendBody('&amp;lt;h1&amp;gt;Hello World&amp;lt;/h1&amp;gt;');
    res.finish();
  }, 2000);
}).listen(8080);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We're using &lt;samp&gt;setTimeout&lt;/samp&gt; to introduce an artificial two second delay to each request. Run the benchmark again - I get 49.68 requests a second, with every single request taking between 2012 and 2022 ms. With a two second delay, the best possible performance for 1000 requests 100 at a time is  &lt;em&gt;1000 requests / (1000 / 100) * 2 seconds = 50 requests a second&lt;/em&gt;. Node hits it pretty much bang on the nose.&lt;/p&gt;

&lt;p&gt;The most important line in the above examples is &lt;code&gt;res.finish()&lt;/code&gt;. This is the mechanism Node provides for explicitly signalling that a request has been fully processed and should be returned to the browser. By making it explicit, Node makes it easy to implement comet patterns like long polling and streaming responses - stuff that is decidedly non trivial in most server-side frameworks.&lt;/p&gt;

&lt;h4&gt;djangode&lt;/h4&gt;

&lt;p&gt;Node's core APIs are pretty low level - it has HTTP client and server libraries, DNS handling, asynchronous file I/O etc, but it doesn't give you much in the way of high level web framework APIs. Unsurprisingly, this has lead to a cambrian explosion of lightweight web frameworks based on top of Node - the &lt;a href="http://wiki.github.com/ry/node"&gt;projects using node page&lt;/a&gt; lists a bunch of them. Rolling a framework is a great way of learning a low-level API, so I've thrown together my own - &lt;a href="http://github.com/simonw/djangode"&gt;djangode&lt;/a&gt; - which brings Django's regex-based URL handling to Node along with a few handy utility functions. Here's a simple djangode application:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;var dj = require('./djangode');

var app = dj.makeApp([
  ['^/$', function(req, res) {
    dj.respond(res, 'Homepage');
  }],
  ['^/other$', function(req, res) {
    dj.respond(res, 'Other page');
  }],
  ['^/page/(\\d+)$', function(req, res, page) {
    dj.respond(res, 'Page ' + page);
  }]
]);
dj.serve(app, 8008);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;djangode is currently a throwaway prototype, but I'll probably be extending it with extra functionality as I explore more Node related ideas.&lt;/p&gt;

&lt;h4&gt;nodecast&lt;/h4&gt;

&lt;p&gt;My main demo in the Full Frontal talk was nodecast, an extremely simple broadcast-oriented comet application. Broadcast is my favourite "hello world" example for comet because it's both simpler than chat and more realistic - I've been involved in plenty of projects that could benefit from being able to broadcast events to their audience, but few that needed an interactive chat room.&lt;/p&gt;

&lt;p&gt;The source code for the version I demoed can be found on GitHub in &lt;a href="http://github.com/simonw/nodecast/tree/no-redis"&gt;the no-redis branch&lt;/a&gt;. It's a very simple application - the client-side JavaScript simply uses jQuery's getJSON method to perform long-polling against a simple URL endpoint:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;function fetchLatest() {
  $.getJSON('/wait?id=' + last_seen, function(d) {
    $.each(d, function() {
      last_seen = parseInt(this.id, 10) + 1;
      ul.prepend($('&amp;lt;li&amp;gt;&amp;lt;/li&amp;gt;').text(this.text));
    });
    fetchLatest();
  });
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Doing this recursively is probably a bad idea since it will eventually blow the browser's JavaScript stack, but it works OK for the demo.&lt;/p&gt;

&lt;p&gt;The more interesting part is the server-side &lt;samp&gt;/wait&lt;/samp&gt; URL which is being polled. Here's the relevant Node/djangode code:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;var message_queue = new process.EventEmitter();

var app = dj.makeApp([
  // ...
  ['^/wait$', function(req, res) {
    var id = req.uri.params.id || 0;
    var messages = getMessagesSince(id);
    if (messages.length) {
      dj.respond(res, JSON.stringify(messages), 'text/plain');
    } else {
      // Wait for the next message
      var listener = message_queue.addListener('message', function() {
        dj.respond(res, 
          JSON.stringify(getMessagesSince(id)), 'text/plain'
        );
        message_queue.removeListener('message', listener);
        clearTimeout(timeout);
      });
      var timeout = setTimeout(function() {
        message_queue.removeListener('message', listener);
        dj.respond(res, JSON.stringify([]), 'text/plain');
      }, 10000);
    }
  }]
  // ...
]);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The wait endpoint checks for new messages and, if any exist, returns immediately. If there are no new messages it does two things: it hooks up a listener on the &lt;samp&gt;message_queue&lt;/samp&gt; EventEmitter (Node's equivalent of jQuery/YUI/Prototype's custom events) which will respond and end the request when a new message becomes available, and also sets a timeout that will cancel the listener and end the request after 10 seconds. This ensures that long polls don't go on too long and potentially cause problems - as far as the browser is concerned it's just talking to a JSON resource which takes up to ten seconds to load.&lt;/p&gt;

&lt;p&gt;When a message does become available, calling &lt;samp&gt;message_queue.emit('message')&lt;/samp&gt; will cause all waiting requests to respond with the latest set of messages.&lt;/p&gt;

&lt;h4&gt;Talking to databases&lt;/h4&gt;

&lt;p&gt;nodecast keeps track of messages using an in-memory JavaScript array, which works fine until you restart the server and lose everything. How do you implement persistent storage?&lt;/p&gt;

&lt;p&gt;For the moment, the easiest answer lies with the NoSQL ecosystem. Node's focus on non-blocking I/O makes it hard (but not impossible) to hook it up to regular database client libraries. Instead, it strongly favours databases that speak simple protocols over a TCP/IP socket - or even better, databases that communicate over HTTP. So far I've tried using CouchDB (with &lt;a href="http://github.com/sixtus/node-couch"&gt;node-couch&lt;/a&gt;) and redis (with &lt;a href="http://github.com/fictorial/redis-node-client"&gt;redis-node-client&lt;/a&gt;), and both worked extremely well. nodecast &lt;a href="http://github.com/simonw/nodecast"&gt;trunk&lt;/a&gt; now uses redis to store the message queue, and provides a nice example of working with a callback-based non-blocking database interface:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;var db = redis.create_client();
var REDIS_KEY = 'nodecast-queue';

function addMessage(msg, callback) {
  db.llen(REDIS_KEY, function(i) {
    msg.id = i; // ID is set to the queue length
    db.rpush(REDIS_KEY, JSON.stringify(msg), function() {
      message_queue.emit('message', msg);
      callback(msg);
    });
  });
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Relational databases are coming to Node. Ryan has a &lt;a href="http://github.com/ry/node_postgres"&gt;PostgreSQL adapter&lt;/a&gt; in the works, thanks to that database already featuring a mature non-blocking client library. MySQL will be a bit tougher - Node will need to grow a separate thread pool to integrate with the official client libs - but you can talk to MySQL right now by dropping in &lt;a href="https://open.nytimes.com/introducing-dbslayer-64d7168a143f"&gt;DBSlayer&lt;/a&gt; from the NY Times which provides an HTTP interface to a pool of MySQL servers.&lt;/p&gt;

&lt;h4&gt;Mixed environments&lt;/h4&gt;

&lt;p&gt;I don't see myself switching all of my server-side development over to JavaScript, but Node has definitely earned a place in my toolbox. It shouldn't be at all hard to mix Node in to an existing server-side environment - either by running both behind a single HTTP proxy (being event-based itself, &lt;a href="http://nginx.net/"&gt;nginx&lt;/a&gt; would be an obvious fit) or by putting Node applications on a separate subdomain. Node is a tempting option for anything involving comet, file uploads or even just mashing together potentially slow loading web APIs. Expect to hear a lot more about it in the future.&lt;/p&gt;

&lt;h4&gt;Further reading&lt;/h4&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://s3.amazonaws.com/four.livejournal/20091117/jsconf.pdf"&gt;Ryan's JSConf.eu presentation&lt;/a&gt; is the best discussion I've seen anywhere of the design philosophy behind Node.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://nodejs.org/api.html"&gt;Node's API documentation&lt;/a&gt; is essential reading.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://debuggable.com/posts/streaming-file-uploads-with-node-js:4ac094b2-b6c8-4a7f-bd07-28accbdd56cb"&gt;Streaming file uploads with node.js&lt;/a&gt; illustrates how well suited Node is to accepting large file uploads.&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://groups.google.com/group/nodejs"&gt;The nodejs Google Group&lt;/a&gt; is the hub of the Node community.&lt;/li&gt;
&lt;/ul&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/async"&gt;async&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/comet"&gt;comet&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/couchdb"&gt;couchdb&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/eventio"&gt;eventio&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/http"&gt;http&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/nodejs"&gt;nodejs&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/nosql"&gt;nosql&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/redis"&gt;redis&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/my-talks"&gt;my-talks&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/tornado"&gt;tornado&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/twisted"&gt;twisted&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/v8"&gt;v8&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/highlights"&gt;highlights&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/annotated-talks"&gt;annotated-talks&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="async"/><category term="comet"/><category term="couchdb"/><category term="eventio"/><category term="http"/><category term="javascript"/><category term="nodejs"/><category term="nosql"/><category term="redis"/><category term="ryan-dahl"/><category term="my-talks"/><category term="tornado"/><category term="twisted"/><category term="v8"/><category term="highlights"/><category term="annotated-talks"/></entry><entry><title>node.js at JSConf.eu (PDF)</title><link href="https://simonwillison.net/2009/Nov/17/nodepdf/#atom-tag" rel="alternate"/><published>2009-11-17T18:07:37+00:00</published><updated>2009-11-17T18:07:37+00:00</updated><id>https://simonwillison.net/2009/Nov/17/nodepdf/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://nodejs.org/jsconf.pdf"&gt;node.js at JSConf.eu (PDF)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
node.js creator Ryan Dahl’s presentation at this year’s JSConf.eu. The principle philosophy is that I/O in web applications should be asynchronous—for everything. No blocking for database calls, no blocking for filesystem access. JavaScript is a mainstream programming language with a culture of callback APIs (thanks to the DOM) and is hence ideally suited to building asynchronous frameworks.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/asynchronous"&gt;asynchronous&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/eventio"&gt;eventio&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/nodejs"&gt;nodejs&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/pdf"&gt;pdf&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ryan-dahl"&gt;ryan-dahl&lt;/a&gt;&lt;/p&gt;



</summary><category term="asynchronous"/><category term="eventio"/><category term="javascript"/><category term="nodejs"/><category term="pdf"/><category term="ryan-dahl"/></entry></feed>