Get Started

Here we will give you a brief introduction of how to use Sparrow in your projects using a simple example. For more advanced examples check our Sparrow by Example section.

Before starting, make sure that you have an updated Elixir distribution on your computer. More info about how to install Elixir here.

Step #1 Create an Elixir project

Use the command mix new from a terminal.

$ mix new sparrow_demo

Step #2 Add Sparrow to your project

To use Sparrow with your projects, edit your project's mix.exs file and add Sparrow as a dependency:

1
2
3
4
5
6
defp deps do
  [
    {:sparrow, "~> 1.0"},
    #{:sparrow, git: "https://github.com/rhumbertgz/sparrow.git", tag: "master"}, # Get the latest version from github
  ] 
end

Step #3 Get Sparrow dependency

Inside the project execute the following command from a terminal:

$ mix deps.get

Step #4 Create a Sparrow actor

Replace the definition of the autogenerated lib/sparrow_demo.ex module with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defmodule SparrowDemo do
  #1 Load Sparrow's macros
  use Sparrow.Actor

  #2 Define a pattern to match only English messages
  pattern my_pattern as {:msg, text, :en}  

  #3 Define a reaction
  reaction my_reaction(l, i, t) do  
	[{text, _} | _] = l
	IO.puts text
  end	  

  #4 Bind the reaction to the pattern
  react_to my_pattern, with: my_reaction 

end

Step #5 Start an iex session inside the project

Inside the project execute the following command from a terminal:

$ iex -S mix

Step #6 Spawn the SparrowDemo actor.

Inside the created iex session execute the following command:

$ { :ok , pid} = SparrowDemo.start

Step #7 Send a message to the SparrowDemo actor.

Inside the created iex session execute the following command:

$ send(pid, {:msg, "Hello, Sparrow", :en})