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 appcrystal 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, runshards installThis 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.runIn the terminal run
crystal run src/sample-app.crWe 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 terminalgit clone https://github.com/amber-crystal/amber.git
cd amber
shards install
makeTo 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-ormAmber 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
get "/hello*" do |ctx| do_something end get "/hello/raze" do |ctx| "hello, raze" endThis 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.newNow, this will work.
get "/hello*", DoSomething.new get "/hello/raze" do |ctx| "hello, raze" end
- Route ambiguity order
Hi Nimmy, great post!
Just a few suggestions:
> an experimental Bash environment running on Windows.
WSL is not experimental (beta) anymore [*]. It’s stable now. However Crystal is still experimental on WSL. [**]
About frameworks:
Currently the most active frameworks for Crystal are Kemal & Amber.
Kemalyst development have been stopped [0], his creator has joined efforts to Amber
Amber-Crystal has been renamed to amberframework [1]
Amber was based on Kemalyst, Kemalyst was based on Kemal, Kemal was based on Frank [2]
[*] – https://blogs.msdn.microsoft.com/commandline/2017/07/28/windows-subsystem-for-linux-out-of-beta/
[**] – https://crystal-lang.org/docs/installation/on_bash_on_ubuntu_on_windows.html
[0] – https://github.com/kemalyst/kemalyst#moved-to-amber
[1] – https://amberframework.org
[2] – https://github.com/manastech/frank
Hi Faustino,
Thank you very much for the feedback and suggestions. Will modify the article.