Jul 1st 2022

Langium in the web browser

Dr. Miro SpönemannDr. Miro Spönemann

We announced Langium as a language engineering framework for Node.js a year ago. But the case for running language servers in the web browser is becoming stronger. That’s why Langium now officially supports browsers as execution runtime. This new feature is demonstrated in a showcase and will be generally available with the upcoming release 0.5.0.

So why is this such a big deal?

Language servers in the backend

The initial assumption of the Language Server Protocol was that the language client (VS Code) and language server run as separate processes on the same machine, communicating via standard I/O. Since the desktop version of VS Code runs on Electron, a Node.js runtime is already shipped with the application, so it’s easy to spawn language servers built with Langium because they run in Node.js, too.

When shifting the text editor to the web with Codespaces, Gitpod, Eclipse Theia or other web IDEs, language servers can still be launched in the same way within the backend. The LSP messages are then transferred between the browser and backend, typically through a Websocket.

What if we don’t need a full-fledged IDE, but just a text editor embedded in a web application? Can we start a single instance of a language server and use that to serve multiple users? The answer is no: this would break the basic LSP assumption of a 1:1 mapping between client and server. The result would be undesirable side effects such as information from documents of one user leaking into the text editor of another user.

The consequence is that we have to spawn one language server per user, and in order to guarantee isolation of the user’s data each server needs to run in a separate infrastructure node. This leads to considerable effort for development and operation of the infrastructure.

Move them to the browser!

Langium is fully built with TypeScript and has only minimal dependencies to Node.js (necessary to read files when running in VS Code or Theia). In order to move Langium to the browser, you can replace the file system access with your own service to resolve workspace documents. In the simplest case you can use a predefined “empty” file system that assumes there is only a single text document, which is the one shown in the text editor.

Running a Langium-based language server in the browser instead of the backend infrastructure brings several advantages:

  • Every user has a dedicated language server instance.
  • Reduced complexity of the backend infrastructure.
  • No network delay, so completion and other text editor features are provided instantly.
  • Users can continue working with the text editor even when offline.

The language server runs in a web worker, which means it does not interfere with page loading, UI updates or any other aspect that is important for the user experience of your web app. Currently the best editor component for the web is Monaco Editor, the code editor that powers VS Code. A detailed guide for embedding Monaco with Langium in a web page will soon be available in the documentation.

State machine language running in the browser

See for yourself

The new showcase page on the Langium website demonstrates a state machine DSL. It’s an example of using Monaco editor with a Langium-based language in a web worker. The source code is available on GitHub. You can expect more examples like this to be published in the following months, including interactivity of the DSLs with the web page.

About the Author

Dr. Miro Spönemann

Dr. Miro Spönemann

Miro joined TypeFox as a software engineer right after the company was established. Five years later he stepped up as a co-leader and is now eager to shape the future direction and strategy. Miro earned a PhD (Dr.-Ing.) at the University of Kiel and is constantly pursuing innovation about engineering tools.

Read more about this topic

read the article

Mar 18th 2024


Irina Artemeva

Run fast, debug easy: Exploring the synergy of Langium and LLVM

Ensuring your language is both executable and debuggable is an interesting challenge. Let's discover how to achieve this using Langium and LLVM.

read the article
watch the videoOpen Video

Mar 7th 2024


Benjamin F. Wilson

Getting started with Langium – Part 7 "Generating Drawing Commands"

In this tutorial Ben will demonstrate how we can generate drawing commands from a MiniLogo program, building on the generator work we’ve already established in the prior tutorial.

watch the video
read the article

Mar 5th 2024


Benjamin F. Wilson

Langium 3.0 is Released!

Langium 3.0 is released! This release brings us new improvements & features, like reduced bundle size, ESM support, and more.

read the article