Skip to content

Commit

Permalink
add Elixir tutorial 1
Browse files Browse the repository at this point in the history
Prior to this commit, the RabbitMQ tutorials contained no Elixir
examples. One _could_ look at the Erlang examples and adapt them, but
that certainly does not result in idiomatic Elixir. This commit adds the
first tutorial and an Elixir project file specifying the `amqp` library
as the dependency for communicating with RabbitMQ via idiomatic Elixir.
  • Loading branch information
jeffweiss committed Dec 22, 2015
1 parent 3b471a4 commit 9f0db0b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions elixir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.mix_tasks
mix.lock
/_build
/deps
34 changes: 34 additions & 0 deletions elixir/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule RabbitmqTutorials.Mixfile do
use Mix.Project

def project do
[app: :rabbitmq_tutorials,
version: "0.0.1",
elixir: "~> 1.1",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end

# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger, :amqp]]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[
{:amqp, "~> 0.1.4"},
]
end
end
17 changes: 17 additions & 0 deletions elixir/receive.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Receive do
def wait_for_messages do
receive do
{:basic_deliver, payload, _meta} ->
IO.puts " [x] Received #{payload}"
wait_for_messages
end
end
end

{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.consume(channel, "hello", nil, no_ack: true)
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"

Receive.wait_for_messages
6 changes: 6 additions & 0 deletions elixir/send.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)
AMQP.Queue.declare(channel, "hello")
AMQP.Basic.publish(channel, "", "hello", "Hello World!")
IO.puts " [x] Sent 'Hello World!'"
AMQP.Connection.close(connection)

0 comments on commit 9f0db0b

Please sign in to comment.