Skip to content

Publish Events

Ahmad Elassuty edited this page Jan 27, 2021 · 2 revisions

Single Event

You can publish a single event using EventRouter.publish method:

event = OrderPlaced.new(order: order, actor: actor)

EventRouter.publish(event)

Multiple Events

Similarly, you can use EventRouter.publish to publish multiple events by passing an array of events instead. Events will be delivered to their destinations in the same order they are passed to the publish method.

event_1 = OrderPlaced.new(order: order_1, actor: actor)
event_2 = OrderPlaced.new(order: order_2, actor: actor)

EventRouter.publish([event_1, event_2])

Custom Delivery Adapter

By default EventRouter.publish will use the configured default delivery adapter. You still can set the adapter per publish call by referencing the adapter name in the adapter attribute. Please make sure that the adapter is registered first.

event = OrderPlaced.new(order: order, actor: actor)

EventRouter.publish(event, adapter: :sidekiq)

One more way!

You can also publish events using the event class publish method:

OrderPlaced.publish(order: order, actor: actor)
Clone this wiki locally