This tutorial is an overview of the main features of the Ocsigen framework. It explains how to program web sites and client-server Web applications in OCaml using Eliom, Lwt, Js_of_ocaml and other libraries of the Ocsigen project. Read it to understand the basics, then refer to each project's manual for more detail.
This programming guide assumes you know the OCaml language, and that you have a working installation of Ocsigen Server, Eliom, Lwt, Js_of_ocaml, (and Ocsigen Widgets for some examples). In particular, you need to have the interfaces (cmi) installed for all these packages (the -dev-packages in some distributions). The best way to install OCaml (on MacOS, Linux ...) is to use the Opam package manager. The installation is described here.
We recommend to register to our mailing list where you can ask your questions and get news from the project. You can also come and chat with us on IRC in channel #ocsigen on freenode.net (e.g. by using their webchat)!
The first section of this page is an overview of the tutorial. The second section of this introduction lists our main software projects.
Overview of the tutorial
The tutorial is divided into several parts, mostly independant:
- Your first cross-platform client-server app in 5 minutes
- Read this tutorial
Quick start: the basics
- A short tutorial explaining how to write a dynamic Web site by generating pages using OCaml functions
- Read tutorial Basic web site
- If you want a quick introduction to client-server Web programming with Eliom
- Read tutorial Eliom apps basics: writing client server widgets
It shows on an example the client-server syntax, and is a good starting point for understanding Eliom's client-server features.
- If you want a full step-by-step tutorial on how to write a client-server Web application
- Read tutorial Client-server application
It describes step by step how to write a client/server collaborative drawing application. You will learn, from the very beginning, how to:
- Create new services
- Output valid HTML
- Send OCaml code to be executed on the client
- Catch mouse events
- Communicate with the server, in both directions
- Create services with non-HTML output
- If you want to build mobile applications:
- Read tutorial Mobile applications with Ocsigen
The tutorial describes how to build a mobile app (e.g., for Android) with the same codebase as for your Web application.
- If you want to write a more traditional Web site, with pages, forms, and sessions:
- Read tutorial Service based Web programming
This tutorial is devoted to server side programming. It shows how to create a new Web site with several pages and user connection. You will learn how to:
- Create a link towards another service
- Create forms
- Register session data or session services
- Create services performing actions (with no output)
- Dynamically register new services (continuation based Web programming)
- If you want a wide overview about Ocsigen, for beginners, in french
- Look at this video
- A short talk, in english, that highlights a few features of Js_of_ocaml and Eliom.
- Look at this video
It describes the most recent features of Eliom.
- If you want to learn more details about Ocsigen
- Read tutorial Miscellaneous features
We will mix the client-server drawing application with the session mechanism and user management to produce a multi-user collaborative drawing application. In this chapter, you will learn how to:
- Integrate a typical Web interaction (links, forms, …) with a client side program.
- Use Macaque for type-safe database interaction
- Add sounds or videos to your application
- Change pages without stopping the client side program
- Connect with external accounts (openID)
- Add an Atom feed
- If you want to have quickly a full application with user management (registration, activation emails, authentication)
- Have a look at Ocsigen-start.
Ocsigen-start is a library and a template for Eliom distillery that contains a working Eliom application with user and right management.
You will also find some other tutorials in the menu on the left.
We also have a list of Quick HOWTOs.
Keep in mind that one of the key feature of Eliom is that it allows one to mix commonplace Web interactions (URLs, forms, links, bookmarks, back button) with dynamic client side features. In particular, the client-side program does not stop when the user clicks a link, sends a form, or presses the back button–yet the user still can save bookmarks on pages! This opens up a wide field of new possibilities, and should facilitate the emergence of new kinds of Web applications.
- If you want a more complete manual
- Read the manual of each project, and/or the API documentation.
Ocsigen software projects
Most projects are independent and can be used for non Eliom-releated applications, for example:
- the cooperative threading library Lwt,
- the TyXML library for generating typed XML,
- the database query library Macaque,
- Ocsigen Server, a full-featured Web server.
Ocsigen Server is a full featured Web server written in modular way. It implements most features of the HTTP protocol, and has a powerful extension mechanism that make easy to plug your own OCaml modules for generating pages. Many extensions are already written:
- to serve static files.
- to create reliable client/server Web applications or Web sites in OCaml using advanced high level concepts.
- allows for more options in the configuration file.
- allows to restrict the access to the sites from the config file (to request coming from a subnet, containing some headers, etc.).
- allows to restrict the access to the sites from the config file using Basic HTTP Authentication.
- serves CGI scripts. It may also be used to serve PHP through CGI.
- used to compress data before sending to the client.
- sets redirections towards other Web sites from the configuration file.
- a reverse proxy for Ocsigen Server. It allows to ask another server to handle the request.
- changes in incoming requests before sending them to other extensions.
- rewrites some parts of the output before sending it to the client.
- allows users to have their own configuration files.
- facilitates server to client communications.
Ocsigen server has a sophisticated configuration file mechanism allowing complex configurations of sites.
Eliom is an extension for Ocsigen Server that allows the creation of reliable client/server Web applications, mobile apps, and traditional dynamic Web sites in OCaml.
Eliom transforms OCaml into a multi-tier language, allowing one to implement (both the server and client parts of) a distributed application entirely in OCaml, as a single program. This radically simplifies the communication between server and client. Applications can run on any Web browser or mobile device (iOS, Android, ...), thus eliminating the need for one custom version per platform.
Eliom also provides reactive pages (generated on server or client), an advanced session mechanism, facilities for server to client communication, support for continuation-based Web programming, etc.
Eliom can also be used for more traditional Web or mobile apps: server-only, single-page applications, REST APIs, etc.
Eliom enables the creation of complex Web sites in very few lines of code, by providing high level programming primitives for many aspects of Web programming: communication between the client and the server, implementation of complex Web interaction, sessions, etc.
Eliom also improves the stability of your application by making extensive use of OCaml's static typing. The compiler checks many things for you, helping you to remove many bugs. For example, the validity of pages (e.g. well-formed HTML) is checked at compile time! Moreover, Eliom statically checks for example that your Web site does not contain any broken links and that the parameters in a link or a form correspond to the service it leads to (and many other things).
A client/server application in one piece of code!
Note that client side features can be mixed with traditional Web interaction (links, forms, bookmarks, back button ...)
Eliom helps you to make your Web application secure by automatically addressing several common security issues (code injection, session fixation ...) and by providing features to implement very secure behaviours (avoiding for example "cross-site request forgery").
Lwt is a cooperative thread library for OCaml. It provides an alternative to the more usual preemptive threads approach for programming concurrent applications, that avoids most problems of concurrent data access and deadlocks. It is used by Ocsigen Server and Eliom and has now become one of the standard ways to implement concurrent applications in OCaml. All your Web sites must be written in Lwt-compatible way!
- The whole language, and most of the standard library are supported.
- The compiler is easy to install: it depends only on Findlib and Lwt.
- The generated code which is independant of Eliom and the Ocsigen Server. You can use it with any Web server.
- You can use a standard installation of OCaml to compile your programs. In particular, you do not have to recompile a library to use it with Js_of_ocaml. You just have to link your program with a specific library to interface with the browser API.
Ocsigen-start is a library and a template of Eliom application, with many common features like user registration, login box, notification system, etc.