Elixir/Phoenix

Elixir

We expect the functional language Elixir to become mainstream in the world of web application development in the 2020s.

Why functional languages? Why Elixir?

This is because the environment surrounding web applications is rapidly becoming more complex. We can now easily handle computers with many cores and huge memory. As a result, we continue to create more complex web applications, whether we like it or not.

In order for a complex system to work properly, it is important to disassemble the system into a collection of simple parts. If possible, it is desirable that these parts be independent of each other and move autonomously and in parallel. Functional languages are suitable for building such systems.

There are many types of functional languages, but we especially expect Elixir. Elixir's grammar is similar to that of popular languages ​​in web development such as Python, PHP, Ruby, and Perl, so the transition hurdles are very low. It also has interesting features that tick the programmer's mind, such as pattern matching and pipeline operators.

Elixir is a new language born in 2012. As of 2019, it is considered to be in the first phase of the technology life cycle (Innovators)1, but we anticipate that somewhere in the 2020s it will move from the Early Adopters phase to the Early Majority phase.

  1. 1 Programming Languages InfoQ Trends Report - October 2019

Features of functional languages

Most programming languages ​​have the concept of a function. Some languages ​​may call it a subroutine, method, or procedure, but they are essentially the same thing.

Therefore, the person who hears the word “functional language” for the first time is uncomfortable. It sounds like a word for programming languages ​​in general. But that's not the case.

In fact, the term functional language does not have a widely accepted and clear definition. So, by comparing two specific languages, Ruby and Elixir, we will explain (part of) the features of functional languages.

Both Ruby and Elixir handle various data (integer, character string, time, etc.) necessary for program execution by naming them. This name is called variable. As an example, let's consider the process of adding "!" to the end of the string pointed to by variable s when its length is 3 or less.

Now s points to the string "abc". This string is stored in a memory location. In Ruby, this data can be altered directly to "abc!". However, Elixir cannot alter the data itself. Instead, it transforms the data to another data. Specifically, "abc" and "!" are concatenated to create the string "abc!", and it is again associated with the variable s.

Although it looks like a minor difference, it actually has a decisive meaning. What happens if two programs X and Y are running in parallel on the same computer, both have variable s that refers to the string "abc" stored in the same location in the memory, and the above process is executed almost simultaneously on both programs?

In the case of Ruby, something bad happens depending on the timing. Immediately after X confirms that the length of the string pointed to by variable s is 3, if Y adds "!" to the same string, X also adds "!" after that, so finally thestring pointed to by variable s will be changed to "abc!!".

But this doesn't happen with Elixir. This is because the original data is kept intact and another data is newly created. In other words, Elixir transforms data without changing it.

Thus, there is a significant difference between alteration and transformation. The biggest feature of the functional language Elixir is that data cannot be altered. Programs written in Elixir proceed through a chain of data transformations. Languages ​​like Elixir are called functional because this data transformation is similar to a mathematical function.

Of course, if you program carefully in Ruby, the above problems will not occur. But humans make mistakes. Elixir is safer in situations where parallel processing is involved.

Phoenix Framework

Phoenix Framework (“Phoenix”) is a web application development framework written in Elixir. It is greatly influenced by Ruby on Rails. In particular, the routing configuration method and how to write controllers and HTML templates are very similar. Although the atmosphere is a little different when it comes to dealing with databases, the fundamental ideas are common.

What we particularly like about Phoenix is ​​that it can handle WebSockets very easily. WebSocket is a technical standard for realizing bidirectional communication in Web applications. Using WebSocket makes it very easy to develop a Single Page Application (SPA).

As for WebSocket, you should also pay attention to Phoenix LiveView. It assembles HTML data on the server side, sends the difference to the browser side with WebSocket, rewrites the DOM, and updates the browser screen.

If you use LiveView, you can write code that was written in JavaScript so far in Elixir. This has two advantages. One is that you don't have to write validation codes in two languages. The other is that most of the processing is done on the server, reducing the risk of secret leakage and unauthorized access.

Development of Teamgenik by Elixir/Phoenix

In 2016, we discovered the possibility of Web development by Elixir/Phoenix and acquired know-how through experimental application development.

In the summer of 2018, we started a new challenge using Elixir/Phoenix. That is Teamgenik.

Teamgenik is a service that gives users the experience of becoming a mini SNS owner, but is characterized by the ability to create mini apps on the browser and add functions to mini-SNS. We have found that Elixir/Phoenix (especially Phoenix LiveView) can be used to efficiently implement these mini apps.

Currently we continue to develop Teamgenik day and night. The preview version will be released in early December 2019. And we plan to release version 1.0 in the spring of 2020.