Using Rails secret weapon: ActiveSupport::Notifications
Tags: rails, ruby, showdev, webdev
Published: 2018-07-03 15:39:44

---
title: Using Rails secret weapon: ActiveSupport::Notifications
published: true
description: Using ruby on rails native publisher/subscriber service
tags: rails, ruby, showdev, webdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/17skv1rfxv0oqean68pe.jpg
---

[Originally posted in my blog](https://blog.hdias.dev/ruby-on-rails-active-support-notifications)

Ruby on Rails has a powerful weapon that not all developers knows about its existence. [ActiveSupport Instrumentations](http://guides.rubyonrails.org/active_support_instrumentation.html) is used by Rails framework to handle events that happen when the application is running.


_The instrumentation API provided by Active Support allows developers to provide hooks which other developers may hook into. There are several of these within the Rails framework. With this API, developers can choose to be notified when certain events occur inside their application or another piece of Ruby code._

Lucky we can use this tool at our disposal.

### How it works

It looks like a `PUB/SUB` pattern.

_In software architecture, publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers. Instead, published messages are characterized into classes, without knowledge of what, if any, subscribers there may be. Similarly, subscribers express interest in one or more classes, and only receive messages that are of interest, without knowledge of what, if any, publishers there are._ - [Wikipedia](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)


Subscribing to an event is pretty easy:

```ruby
# config/initializers/events.rb
ActiveSupport::Notifications.subscribe "my_custom.event" do |*args|
  event = ActiveSupport::Notifications::Event.new(*args)
end
```

The event variable will contain something like this

```ruby
=> #<ActiveSupport::Notifications::Event:0x0000559258433df8
 @children=[],
 @duration=nil,
 @end=2018-07-03 12:47:46 +0000,
 @name="my_custom.event",
 @payload={:foo=>"bar"},
 @time=2018-07-03 12:47:46 +0000,
 @transaction_id="37bb01f75132e0c0505publisher6">
```

Notice the `@payload`? We can send additional data within the event.


### Instrumenting an event
There is a method called `instrument` that publish the event.

You can use it like this:

```ruby
ActiveSupport::Notifications.instrument "my_custom.event", { foo: "bar" }
```

Easy right?

### Real world example

Let's say that you have an external service that handles application metrics, such as Segment.IO, Keen IO, and many others.

You can use this pattern to send events to one of this services.

```ruby
# config/initializers/events.rb
# We can use regex to subscribe!
ActiveSupport::Notifications.subscribe /metrics/ do |*args|
  event = ActiveSupport::Notifications::Event.new(*args)
  MyMetricsService.send(event.name, event.payload)
end

# app/controllers/my_controller.rb
def create
  # some code
  ActiveSupport::Notifications.instrument "metrics.item_purchased", { uid: item.id }
end
```

You can read more about [ActiveSupport Instrumentation here](http://guides.rubyonrails.org/active_support_instrumentation.html)

Hope it helps :)

---

Photo by [Ciprian Boiciuc on Unsplash](https://unsplash.com/photos/TrNSWatUW5g?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)