Skip to main content
Features rarely live alone. A search index needs to know when a size guide changes; a product information system wants to hear about new ones. In Spree the integration surface is the events system: models publish events as things happen, and you react without touching core code. There are two ways to consume an event, and they serve different audiences:

Step 1: Lifecycle events are already on

Core models publish *.created, *.updated and *.deleted automatically, and so does yours: the generator in step 1 put this line in the model:
server/app/models/spree/size_guide.rb
So size_guide.created, size_guide.updated and size_guide.deleted fire after the matching transaction commits. The event names start with the model name in snake case, size_guide, not with the ID prefix sg. The payload is the record serialized with the v3 serializer you generated in step 1 — the events system resolves it by naming convention. Every consumer sees the same JSON shape your API serves, which is the point: one contract, not two.

Step 2: React inside the app

Generate a subscriber:
This creates app/subscribers/size_guide_sync_subscriber.rb, a spec stub, and — crucially — registers it in config/initializers/spree.rb. Subscribers are not auto-discovered: one that never gets appended to Spree.subscribers is a silent no-op, which is why the generator owns that step. Re-runs are idempotent. Restart the server with spree restart so the registration takes effect. Fill in the handler:
server/app/subscribers/size_guide_sync_subscriber.rb
SearchIndexer stands for your own client code. Two things worth understanding:
  • The payload is the record, already serialized. It is the same JSON your API returns, so event.payload['name'] is there without a database query. IDs in it are public ones (sg_UkLWZg9DAJ); when you do need the full record, look it up with Spree::SizeGuide.find_by_prefix_id(event.payload['id']) rather than treating the id as a row number.
  • Subscribers run asynchronously by default. Each handle call is an ActiveJob, so a slow third-party API never blocks the request that triggered it. Pass subscribes_to 'size_guide.created', async: false only when you genuinely need it synchronous.
Rename a size guide in the dashboard and watch the job run.

Step 3: Publish a custom event

Lifecycle events cover persistence. Custom events express domain moments — something meaningful happened, not just a row changed. Declare each one on the model whose record is its payload:
server/app/models/spree/size_guide.rb
The model’s name is added in front, so this declares size_guide.published. Then publish it where the moment happens:
Publishing an event no model declared raises an error in development and test, so a typo in an event name fails a spec instead of going unnoticed. The payload defaults to the serializer output; pass your own hash as a second argument when the event needs different data. Subscribers consume it exactly like any built-in event, and declared events are offered to webhooks too.

Step 4: Deliver to external systems with webhooks

When the consumer is a system you do not deploy your own code into, use a webhook instead. In the dashboard go to Settings → Webhooks, add an endpoint URL, and select the events to deliver — size_guide.created and anything else publishing in your store. Each delivery is an HTTP POST carrying the event name and the same serialized payload. The endpoint’s signing secret is shown once when you create it: store it in the receiving system and verify every request against it, so the receiver can prove the call came from your store. Failed deliveries are retried with increasing delays, and the dashboard shows the delivery history per endpoint, which is where to look when a third party says they never received something.
Webhooks need no code in your app at all. Reach for a subscriber only when the reaction needs application context or credentials you would rather not hand to a third party. See events and webhooks.

Next step

The feature is complete across every layer. Now prove it works: Testing.