Luv — cross-platform native I/O: a binding to libuv


#1

(This is a cross-post from the OCaml forum)

I am pleased to announce the first release of Luv, a binding to libuv. libuv is the cross-platform C library inside Node.js that does I/O, wraps system calls, and drives Node’s main loop.

let () = {
  let timer = Luv.Timer.init()->Stdlib.Result.get_ok;
  ignore(Luv.Timer.start(timer, 1000, () =>
    print_endline("Hello, world!")));

  print_endline("Waiting...");
  ignore(Luv.Loop.run());
};

Luv exposes sockets, files, subprocesses, FS events, and many other OS APIs. Indeed, Luv is an independent alternative to the standard Unix, and does not depend on Unix. The easiest way to get an idea of all that is available is to quickly scroll through the list of modules in the API docs.

You are invited to glance through all of Luv’s documentation:


libuv is focused primarily on event-driven programming, which makes it similar to Async or Lwt. However, it does not offer a promise data type — the API is written in terms of callbacks.

libuv also supports multithreading and multiprocessing. In particular, libuv allows running multiple event loops in multiple threads, which makes it a ready candidate for use with a multicore runtime.


A major advantage of libuv is that, because it is a core module of Node, it is well-maintained and supports many platforms. Luv inherits these properties.

Luv uses ctypes to greatly minimize the amount of C code in the repo. A thorough test suite checks return values, I/O effects, and interaction with the OCaml garbage collector and threading runtime. Luv vendors libuv to avoid version conflicts, and, when you install Luv, it automatically builds its internal libuv.

One of the main goals of Luv is to be easy to integrate into larger projects. For example, there may be an experimental version of Lwt based on it. To that end, Luv is as unopinionated as possible. It sticks closely to the libuv API, making no unnecessary design decisions, and deviating only where changes are necessary to integrate with OCaml, or where libuv has an awkward API due to the limitations of C.


And with all that… Happy systems programming :slight_smile:


#2

Loved the extensive docs. Looking forward to trying it in a weekend!


#3

Luv is truly awesome, thanks for making this! I’m certain this will be a useful building block for making Node-like network libraries, which will be more familiar to the community.