Skip to content

Commit e9b164f

Browse files
committed
Document linking to streams by metadata
Issue: #346 #221 [ci skip]
1 parent a68c116 commit e9b164f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

railseventstore.org/source/docs/correlation_causation.html.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,39 @@ class AddProductCommand < Struct.new(:message_id, :product_id)
151151
end
152152
```
153153

154+
## Building streams based on correlation id and causation id
155+
156+
You can use `RailsEventStore::LinkByCorrelationId` (`RubyEventStore::LinkByCorrelationId`) and `RailsEventStore::LinkByCausationId` (`RubyEventStore::LinkByCausationId`) to build streams of all events with certain correlation or causation id. This makes debugging and making sense of a large process easier to see.
157+
158+
```ruby
159+
Rails.application.configure do
160+
config.to_prepare do
161+
Rails.configuration.event_store = event_store = RailsEventStore::Client.new
162+
event_store.subscribe_to_all_events(RailsEventStore::LinkByCorrelationId.new)
163+
event_store.subscribe_to_all_events(RailsEventStore::LinkByCausationId.new)
164+
end
165+
end
166+
```
167+
168+
After publishing an event:
169+
170+
```ruby
171+
event = OrderPlaced.new
172+
event_store.publish(event)
173+
```
174+
175+
you can read events caused by it:
176+
177+
```ruby
178+
event_store.read.stream("$by_causation_id_#{event.event_id}")
179+
```
180+
181+
and events correlated with it:
182+
183+
```ruby
184+
event_store.read.stream("$by_correlation_id_#{event.correlation_id || event.event_id}")
185+
```
186+
154187
## Thanks
155188

156189
Image thanks to [Arkency blog](https://blog.arkency.com/correlation-id-and-causation-id-in-evented-systems/)

0 commit comments

Comments
 (0)