-
Notifications
You must be signed in to change notification settings - Fork 1
Description
How to capture external events, such as those recorded by an IoT device.
Events can be directly appended to an event stream, using Commanded.EventStore, without needing to be produced by an aggregate from a command. An event handler can be used to react to these external events. It may dispatch a command as a reaction to an event.
-
Define an integration event struct which can be used for any external event without needing to define a custom struct for each type.
defmodule MyApp.Integration.Event do defstruct [:source, :type, :data] end
The type of the external event will be indicated by the
typefield. This allow loose coupling which is preferred when dealing with events from a third party, external system. -
Append events to a stream (e.g.
devices/device-1234) usingCommanded.EventStore.append_to_stream/4and an integration Commanded Application instance.source = "devices/device-1234" events = [ %Commanded.EventStore.EventData{ event_type: "Elixir.MyApp.Integration.Event", data: %MyApp.Integration.Event{ source: source, type: "ThirdPartyService.SomeExternalEvent" data: %{ "key1" => "value1", "key2" => "value2" } }, metadata: %{} } ] :ok = Commanded.EventStore.append_to_stream(MyApp.Integration.App, source, :any_version, events)
As these are external events we are storing in raw format we can use
:any_versionto skip concurrency control checking.