EventStore.subscribe
subscribe
, go back to EventStore module for more information.
Specs
subscribe(stream_uuid :: String.t(), opts :: transient_subscribe_options()) :: :ok | {:error, term()}
Create a transient subscription to a given stream.
stream_uuid
is the stream to subscribe to. Use the$all
identifier to subscribe to events from all streams.opts
is an optional map providing additional subscription configuration:name
the name of the event store if provided tostart_link/1
.selector
to define a function to filter each event, i.e. returns only those elements for which fun returns a truthy valuemapper
to define a function to map each recorded event before sending to the subscriber.
The calling process will be notified whenever new events are appended to
the given stream_uuid
.
As the subscription is transient you do not need to acknowledge receipt of
each event. The subscriber process will miss any events if it is restarted
and resubscribes. If you need a persistent subscription with guaranteed
at-least-once event delivery and back-pressure you should use
EventStore.subscribe_to_stream/4
.
Notification message
Events will be sent to the subscriber, in batches, as {:events, events}
where events is a collection of EventStore.RecordedEvent
structs.
Example
{:ok, subscription} = EventStore.subscribe(stream_uuid)
# receive first batch of events
receive do
{:events, events} ->
IO.puts "Received events: " <> inspect(events)
end