Skip to content

Commit 27616bf

Browse files
committed
add elixir streams example
1 parent c26adae commit 27616bf

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ ruby*/gems/
1818
venv/*
1919
ruby*/rubygems*
2020

21+
elixir-stream/deps
22+
elixir-stream/_build
23+
elixir-stream/.elixir_ls
24+
2125
java*/.idea/workspace.xml
2226
java*/.idea/encodings.xml
2327
*~

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The following ports are available:
3030
* [Common Lisp](./common-lisp)
3131
* [Dart](./dart)
3232
* [Elixir](./elixir)
33+
* [Elixir (Streams)](./elixir-stream)
3334
* [Erlang](./erlang)
3435
* [Go](./go)
3536
* [Haskell](./haskell)

elixir-stream/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Elixir code for RabbitMQ tutorials
2+
3+
Here you can find Elixir code examples from [RabbitMQ tutorials](https://www.rabbitmq.com/getstarted.html).
4+
5+
## Requirements
6+
7+
These examples use the [`VictorGaiva/rabbitmq-stream`](https://github.com/VictorGaiva/rabbitmq-stream) client library.
8+
9+
The dependencies are installed during the exection of the examples using `Mix.install/1`
10+
11+
## Code
12+
13+
Code examples are executed via `elixir`:
14+
15+
Tutorial one: "Hello World!":
16+
17+
elixir publish.exs
18+
elixir consume.exs
19+
20+
To learn more, see [`VictorGaiva/rabbitmq-stream`](https://github.com/VictorGaiva/rabbitmq-stream).

elixir-stream/consume.exs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#! /usr/bin/env elixir
2+
require Logger
3+
4+
# Installing the rabbitmq_stream Library
5+
Mix.install([
6+
{:rabbitmq_stream, "~> 0.4.1"}
7+
])
8+
9+
# First we start a Connection to the RabbitMQ Server
10+
{:ok, connection} = RabbitMQStream.Connection.start_link()
11+
12+
# We can assume the stream already exists as this sample is meant to be run after the 'publish.exs' sample
13+
14+
# Now we can subscribe to the stream, receiving up to 1 chunk.
15+
{:ok, subscription_id} =
16+
RabbitMQStream.Connection.subscribe(connection, "my_stream", self(), :first, 1)
17+
18+
# Now we can consume the messages
19+
receive do
20+
# Each 'deliver' data comes inside a Chunk, which may contain multiple messages
21+
{:deliver, %{subscription_id: ^subscription_id, osiris_chunk: chunk}} ->
22+
for message <- chunk.data_entries do
23+
Logger.info("Received: #{inspect(message)}")
24+
end
25+
end

elixir-stream/publish.exs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#! /usr/bin/env elixir
2+
require Logger
3+
4+
# Installing the rabbitmq_stream Library
5+
Mix.install([
6+
{:rabbitmq_stream, "~> 0.4.1"}
7+
])
8+
9+
# First we start a Connection to the RabbitMQ Server
10+
{:ok, connection} = RabbitMQStream.Connection.start_link()
11+
12+
# We can assume the stream doesn't exist yet, and attempt to create it. If it already exists,
13+
# it should be still be good to go.
14+
RabbitMQStream.Connection.create_stream(connection, "my_stream")
15+
16+
# Before publishing a message, we need to declare a producer. It is required by the
17+
# RabbitMQ Sever to prevent message duplication.
18+
{:ok, producer_id} =
19+
RabbitMQStream.Connection.declare_producer(connection, "my_stream", "my_producer")
20+
21+
# Each producer has a sequence number, that must must be published with the message, and
22+
# incremented after each message.
23+
{:ok, sequence_number} =
24+
RabbitMQStream.Connection.query_producer_sequence(connection, "my_stream", "my_producer")
25+
26+
# Now we can publish a message. Note that we only specify the producer_id and sequence number.
27+
# The target Stream is already tracked by the server based on the producer_id.
28+
:ok =
29+
RabbitMQStream.Connection.publish(connection, producer_id, sequence_number + 1, "Hello, World!")
30+
31+
Logger.info("Published: \"Hello, World!\"")

0 commit comments

Comments
 (0)