
Ocsigen: A Full OCaml Framework for Websites and Apps

Communications Officer
Are you interested in using a functional programming language like OCaml for web development? This post will give you an overview of a great resource you might want to consider (if you're not using it already!). Ocsigen is a collection of projects that provide a complete framework for the OCaml developer looking to create websites and mobile apps. It’s got you covered, from simple server-side websites to client-side programs and complex client-server applications.
The Start of Ocsigen
So, how did the idea of a web framework for OCaml come about? Ocsigen began as a research project under the Research Institute for Foundations of Computer Science (IRIF), aiming to introduce support alongside the requisite tools for web development in OCaml. At the time of its inception (2005), there were no mature web frameworks for the language, and the founders Vincent Balat and Jérôme Vouillon wanted to implement a full framework that would support professional-level projects at scale.
The reasoning behind choosing OCaml for this project was to take advantage of the language’s expressive type system. This system can check the properties of programs at compile time, reduce development time, make it easier to refactor code and add new features, and ensure that existing features are working correctly.
The research paper titled Ocsigen: Typing Web Interaction With Objective Caml, written by Vincent and based on the broader Ocsigen project, makes two claims on which the design of Ocsigen is founded: firstly, that web languages could (and should) take greater advantage of static typing, and second, that functional programming is a good fit for web programming.
Continuations, JavaScript, and Multi-Tier Programming
Their first claim meant that they focussed on ensuring the correctness of generated pages using static typing and being able to statically check that links and forms match dynamic pages operating as typed services.
The second claim was inspired by several articles positing that web programming would benefit from taking advantage of continuations. Continuation is a concept in programming that refers to an abstract representation of the control state of a computer program. Functional programming can use and manipulate continuations in a straightforward and easy way. In Web programming, continuations are a very elegant solution to what is known as “the back button problem”. Vincent noticed that, at the time, few tools took advantage of this opportunity and determined to implement continuations along with other functional programming features in Ocsigen. As a result, the framework has added whole new functionalities to aspects of traditional web programming using continuations: typed forms, typed links, and advanced session handling using scoped references.
Furthermore, the Ocsigen team implemented one of the first compilers to JavaScript, enabling developers to use OCaml both on the server and client side.
Finally, the Ocsigen team invented multi-tier programming. In multi-tier programming, both ‘sides’ of an application are written as a single program. This makes communication between the server and client straightforward, allowing the user to generate web pages from the server or the client side (with the application being indifferent to which side you choose). For the first time, this allowed developers to mix app development and traditional website development in the same app! It also opens up the possibility for you to write mobile applications with the exact same code as you write web applications.
What is Ocsigen?
Ocsigen consists of several independent open-source projects, all available on GitHub. In this post, we’ll introduce you to the most used ones, but if you want to discover more, check out their website.
- Lwt: is a concurrency library that handles I/O operations using promises. Promises are references that will be filled asynchronously. One of the biggest advantages when calling a function that returns a promise is that it will not require a new stack or process. This ensures a high-speed, efficient call. Recently, Ocsigen is moving to using Eio as a concurrency library.
- TyXML: is a library used to build statically correct HTML and SVG documents. It functions largely like HTML with a combinator implementing HTML attributes and elements, so it’s easy to pick up and use for most programmers. However, with TyXML, you can use OCaml to manipulate elements, and invalid markup text will yield a type error, which helps you write cleaner code. There are standalone examples available in the TyXML GitHub repo to help you get started.
- Js_of_ocaml: is a compiler for converting OCaml bytecode to JavaScript, allowing users to run pure OCaml programs in JavaScript environments. Js_of_ocaml is easy to install, works out-of-the-box with a lot of existing bindings to browser APIs, and generates performant programs. In addition, the Js_of_ocaml repo now also contains wasm_of_ocaml, another compiler (originally a fork of the former) that compiles to WebAssembly targets.
- Eliom: is a framework that enables users to implement multi-platform applications on web browsers and mobile devices in a modern programming style. It is not a new language but rather an extension of OCaml. Its main features are that it provides high-level expressive concepts that enable developers to program complex behaviours in very few lines of code; supports higher security by implementing the OCaml type system and checking properties of applications at compile time; and makes it possible to write client-server applications as single programs using a multi-tier extension of OCaml.
- Ocsigen Server: is a web server, as the name suggests. It is available as an executable and as a library and supports a wide variety of services, including static files, redirection, reverse proxy, CORS, page compression, authentication, and more. -Ocsigen Toolkit: is a collection of useful resources like widgets and other utilities that users may want to use for their web applications. Usefully, most of the widgets are compatible with mobile programming thanks to their ability to be produced on the server side of the client using the same code.
- Ocsigen Start: is an application template containing many standard features, including user management, notifications, and code examples. It can be used to learn the Ocsigen framework or to quickly create a prototype using it. There is a demo online that you can try before installing the application, and it’s also available on mobile.
A quick note on Eio: Eio is an effects-based direct-style I/O library compatible with OCaml 5 including multicore. Direct-style concurrency enables the user to write code in a natural style without taking into consideration which code is concurrent and which isn’t (thereby eliminating the well-known function colouring problem). Eio emphasises performance, making the most of new kernel I/O interfaces for enhanced parallelism efficiency and security, with parts of it being formally verified and using low-level and high-level interfaces. You can read more in one of our blog posts on Eio.
BeSport
BeSport is a social networking platform created for sports clubs, amateurs, and pros. Teams and clubs can share content with their fans, and users can follow results, news, and statistics for their favourite professional teams or their own amateur leagues.
Until Next Time
You can connect with us on Bluesky, Mastodon, Threads, and LinkedIn or sign up for our mailing list to stay updated on our latest projects. We look forward to hearing from you!
Open-Source Development
Tarides champions open-source development. We create and maintain key features of the OCaml language in collaboration with the OCaml community. To learn more about how you can support our open-source work, discover our page on GitHub.
Stay Updated on OCaml and MirageOS!
Subscribe to our mailing list to receive the latest news from Tarides.
By signing up, you agree to receive emails from Tarides. You can unsubscribe at any time.