Skip to content

Commit

Permalink
add Elixir source for tutorial 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffweiss committed Dec 22, 2015
1 parent aa20759 commit 37086be
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions elixir/emit_log.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)

message =
case System.argv do
[] -> "Hello World!"
words -> Enum.join(words, " ")
end

AMQP.Exchange.declare(channel, "logs", :fanout)
AMQP.Basic.publish(channel, "logs", "", message)
IO.puts " [x] Sent '#{message}'"

AMQP.Connection.close(connection)
21 changes: 21 additions & 0 deletions elixir/receive_logs.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule ReceiveLogs do
def wait_for_messages(channel) do
receive do
{:basic_deliver, payload, _meta} ->
IO.puts " [x] Received #{payload}"

wait_for_messages(channel)
end
end
end

{:ok, connection} = AMQP.Connection.open
{:ok, channel} = AMQP.Channel.open(connection)

AMQP.Exchange.declare(channel, "logs", :fanout)
{:ok, %{queue: queue_name}} = AMQP.Queue.declare(channel, "", exclusive: true)
AMQP.Queue.bind(channel, queue_name, "logs")
AMQP.Basic.consume(channel, queue_name, nil, no_ack: true)
IO.puts " [*] Waiting for messages. To exit press CTRL+C, CTRL+C"

ReceiveLogs.wait_for_messages(channel)

0 comments on commit 37086be

Please sign in to comment.