- 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 CrystalApplications 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
Kemalhttps://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 KemalIn our app, Open in an editor and add Kemal as a dependency in the shard.yml file. To get dependencies, run
shards 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)
Amethysthttps://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.
Amberhttps://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
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.
Kemalysthttps://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.
RazeRefer 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