Skip to content

Commit

Permalink
docs: Improving documentation on mudes themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorGaiva committed Mar 4, 2024
1 parent 4ea0cb1 commit fa30933
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 16 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ Elixir Client for [RabbitMQ Streams Protocol](https://www.rabbitmq.com/streams.h

## Usage

## Installation

The package can be installed by adding `rabbitmq_stream` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:rabbitmq_stream, "~> 0.4.0"},
# ...
]
end
```

### Consuming from stream

First you define a connection
Expand Down Expand Up @@ -62,7 +75,7 @@ You can define a `Producer` module like this:
```elixir
defmodule MyApp.MyProducer do
use RabbitMQStream.Producer,
stream: "stream-01",
stream_name: "stream-01",
connection: MyApp.MyConnection
end
```
Expand All @@ -73,19 +86,6 @@ Then you can publish messages to the stream:
MyApp.MyProducer.publish("Hello World")
```

## Installation

The package can be installed by adding `rabbitmq_stream` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:rabbitmq_stream, "~> 0.4.0"},
# ...
]
end
```

## Configuration

The configuration for the connection can be set in your `config.exs` file:
Expand Down
2 changes: 1 addition & 1 deletion guides/setup/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ To prevent message duplication, RabbitMQ requires us to declare a named Producer
```elixir
defmodule MyApp.MyProducer
use RabbitMQStream.Producer,
stream: "my_stream",
stream_name: "my_stream",
connection: MyApp.MyConnection
end
```
Expand Down
52 changes: 52 additions & 0 deletions lib/consumer/consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,58 @@ defmodule RabbitMQStream.Consumer do
* `:offset_reference`
* `:private`
# Decoding
You can declare a function for decoding each message by declaring a `decode!/1` callback as follows:
defmodule MyApp.MyConsumer do
use RabbitMQStream.Consumer,
connection: MyApp.MyConnection,
stream_name: "my_stream",
initial_offset: :first
@impl true
def decode!(message) do
Jason.decode!(message)
end
end
Or by passing a `:serializer` option to the `use` macro:
defmodule MyApp.MyConsumer do
use RabbitMQStream.Consumer,
connection: MyApp.MyConnection,
stream_name: "my_stream",
initial_offset: :first,
serializer: Jason
end
The default value for the `:serializer` is the module itself, unless a default is defined at a higher level of the
configuration. If there is a `decode!/1` callback defined, it is always used
# Properties
You can provide additional properties to the consumer to change its behavior, by passing `:properties`.
## Single active consumer
To use it, you must provide a "group_name". The server manages each consumer so the only one will of each group
will be receiving chunks at a time.
Although there is only one Consumer active, we must provide the server the offset a consumer starts on when being upgraded
to the being the active one. To do so you must implement the `handle_update/2` callback, which must return a `{:ok, offset}` tuple.
@impl true
def handle_update(_, :upgrade) do
{:ok, :last}
end
@impl true
def handle_update(_, :downgrade) do
# Must return something when being downgraded, but it is not used by the server.
# Could be useful to use some external logic to persist the offset somewhere,
# so that it can be queried by the other consumer that is being upgraded
{:ok, :last}
end
"""
defmacro __using__(opts) do
defaults = Application.get_env(:rabbitmq_stream, :defaults, [])
Expand Down
28 changes: 27 additions & 1 deletion lib/producer/producer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ defmodule RabbitMQStream.Producer do
* `:stream_name`
* `:reference_name`
# Encoding
You can declare a function for encoding each message by declaring a `encode!/1` callback as follows:alarm_handler
defmodule MyApp.MyProducer do
use RabbitMQStream.Producer,
stream_name: "my-stream",
connection: MyApp.MyConnection
@impl true
def encode!(message) do
Jason.encode!(message)
end
end
Or by passing a `:serializer` option to the `use` macro:
defmodule MyApp.MyProducer do
use RabbitMQStream.Producer,
stream_name: "my-stream",
connection: MyApp.MyConnection,
serializer: Jason
end
The default value for the `:serializer` is the module itself, unless a default is defined at a higher level of the
configuration. If there is a `encode!/1` callback defined, it is always used
"""

defmacro __using__(opts) do
Expand Down Expand Up @@ -205,7 +231,7 @@ defmodule RabbitMQStream.Producer do
message["key"]
end
"""
@callback filter_value(message :: term()) :: String.t()
@callback filter_value(message :: term()) :: String.t() | nil

@optional_callbacks [before_start: 2, filter_value: 1]
defstruct [
Expand Down

0 comments on commit fa30933

Please sign in to comment.