Web Frameworks in Crystal

Features

  • Native WebSocket support
  • Compiles to efficient native code
  • Statically typed => if any errors it will let you know at compile time.
  • Less memory consumption

Web frameworks in Crystal

Applications developed using framework are easy to maintain and upgrade at a lower cost. This article lets you get familiar with some of the most popular frameworks of Crystal. Install Crystal to get started. https://crystal-lang.org/docs/installation/index.html Create our Crystal app
crystal init app sample-app

Kemal

https://github.com/kemalcr/kemal To know the true potential of Crystal, let’s familiarise with Kemal, most popular Crystal framework. It’s a Sinatra inspired framework.

Install Kemal

In our app, Open in an editor and add Kemal as a dependency in the shard.yml file. To get dependencies, run
shards install
This will install Kemal. This is similar to adding gems in gem file and bundle install in Rails. In the file sample-app.cr created in the src directory, substitute the following
require "kemal"
# Matches GET "http://host:port/"
get "/" do
"Hello World!"
end
Kemal.run
In the terminal run
crystal run src/sample-app.cr
We can see logs in the terminal as Kemal is ready in localhost. Using WebSockets is quite easy with Kemal. (Will be explaining it in our another blog)

Amethyst

https://github.com/crystal-community/amethyst Like Sinatra inspired framework Kemal for crystal, Amethyst is Rails-inspired framework for crystal which is extremely fast and flexible in application development. Installation is similar as Kemal; add as a dependency in the shard.yml file. To start using Amethyst, require it in project code. For fastest and lightweight framework in crystal we could choose Kemal or if interested in something more like Rails rather than speed Amethyst can be a better option. For controllers and to describe routes, Amethyst has a Rails-like approach. After Kemal, Amethyst is the popular framework of Crystal.

Amber

https://github.com/Amber-Crystal/amber Amber framework was developed inspired by Kemal, Rails, Django and other popular frameworks. Amber follows the concepts and conventions of these already successful frameworks. It implements MVC pattern, implementing ORM (Object-Relational-Model) in Crystal. To use Amber, after installing crystal, download and install amber In the terminal
git clone https://github.com/amber-crystal/amber.git
cd amber
shards install
make
To create new application in Amber refer to official doc https://amber-crystal.gitbooks.io/amber/content/getting-started/Installation/create-new-app.html To provide an ORM Model in Crystal add the library Granite:: ORM to your project dependencies in the shard.yml file.
dependencies:
  granite_orm:
    github: Amber-Crystal/granite-orm
Amber provides WebSocket support for real-time communication which is very simple, and only requires a Socket, a Channel, and client-side interaction using javascript. Amber is inspired by Kemal, which has inbuilt WebSocket support.

Kemalyst

https://github.com/kemalyst/kemalyst Kemalyst is a Crystal lang framework based on Kemal. Kemalyst is also a Rails similar framework like Amber. Following the MVC pattern, Kemalyst supports MySQL, PG, and SQLite. Views are handled via kilt, generic template interface for Crystal. Kemalyst also provides support for WebSockets, jobs to perform background tasks using sidekiq.cr, the simple and efficient job processing for Crystal.

Raze

Refer https://github.com/samueleaton/raze for installation which is as simple as Kemal. Raze framework is a modular, light web framework for Crystal. Raze can handle approximately 120,000 requests per second, which is way more than Kemal(90,000 requests per second). Raze implements a middleware-centric design for greater modularity. Putting more logic inside reusable middlewares considerably reduces the need for route blocks. When writing routes in Raze there are two things to be noted:
  • Two matching routes that both having a block is not possible
For example, this will fail
get "/hello*" do |ctx|
  do_something
end
get "/hello/raze" do |ctx|
  "hello, raze"
end
This is where middleware is used if you want to do something before the second route. If you wanted to add a custom DoSomething middleware to your get “/hello*” route, it’s as simple as the following:
get "/hello*", DoSomething.new
Now, this will work.
get "/hello*", DoSomething.new
get "/hello/raze" do |ctx|
  "hello, raze"
end
  • Route ambiguity order
Raze will make sure that any matching routes are in order from most to least ambiguous. The less specific path must be defined before the more specific path as shown in the above example or else Raze will raise route exceptions.

Limitations

Crystal language is relatively new and therefore the community and ecosystem need more time to mature. Crystal lang is currently implemented as a single-threaded process. This means that you probably can’t max out all CPUs of your machine with just a single process. If we need scaling in the future for a real product, then Erlang or Elixir would be a much better choice than Crystal. Compared to Erlang, Golang, and Clojure, Crystal is less concurrent. Also, Crystal not supporting Windows yet is another limitation. But in Windows 10 you can (experimentally) try Crystal using Bash on Ubuntu on Windows, an experimental Bash environment running on Windows.

Conclusion

All these frameworks are growing rapidly and expecting further contributions. Yet there are many other frameworks such as Lattice-core, luckyframework which is in the early stages of development. Lattice-core is built on Kemal framework. All of them focusing on speed, modularity, and simplicity. None of Crystal’s frameworks are mature enough to be a full-blown web framework yet. But still, Kemal is my favorite over all other frameworks. There are more companies already using Crystal / Kemal in production now. https://github.com/crystal-lang/crystal/wiki/Used-in-production Kemal is been extremely fast and responsive by using just 1/50 of the resource. Kemal’s superb performance for Websockets and easily extensible middlewares are appreciable. Also, deploying a Kemal app on Heroku is as simple as follows: https://redpanthers.co/deploying-crystal-app-production-using-heroku

Reference

 ]]>