Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Recipe] External events #14

Open
slashdotdash opened this issue Nov 29, 2019 · 0 comments
Open

[Recipe] External events #14

slashdotdash opened this issue Nov 29, 2019 · 0 comments

Comments

@slashdotdash
Copy link
Member

slashdotdash commented Nov 29, 2019

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.

  1. 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 type field. This allow loose coupling which is preferred when dealing with events from a third party, external system.

  2. Append events to a stream (e.g. devices/device-1234) using Commanded.EventStore.append_to_stream/4 and 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_version to skip concurrency control checking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant