Push notifications from Ruby
Your Ruby app does something important. You find out when you check the logs or the Stripe dashboard.
Here’s how to make it tell you directly.
The simplest version: net/http
No gem needed. trigger.fyi is a plain HTTP endpoint:
require "net/http"
require "uri"
def notify(title)
key = ENV["TRIGGER_FYI_SECRET_KEY"]
return unless key
uri = URI("https://trigger.fyi/#{key}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.open_timeout = 3
http.read_timeout = 3
req = Net::HTTP::Post.new(uri)
req["Content-Type"] = "text/plain"
req.body = title
http.request(req)
rescue => e
# fire-and-forget — swallow errors
endWith the gem
gem install trigger-fyi
# or add to Gemfile: gem "trigger-fyi"require "trigger_fyi"
# Normal notification
Fyi.call("Job complete")
# With detail
Fyi.call("Job complete", body: "847 records in 4m 12s")
# With metadata
Fyi.call("New signup", body: "[email protected]", plan: "pro")
# Feed only — no push
Fyi.log("Cache warmed", body: "12,400 keys")
# High urgency
Fyi.critical("Payment failed", body: customer.email)The gem reads TRIGGER_FYI_SECRET_KEY from the environment. Errors are swallowed — it never raises.
In Rails
After the response is sent, fire the notification in a background thread or job:
class SignupsController < ApplicationController
def create
@user = User.create!(signup_params)
render json: @user
# Fire after response — don't block the request
Thread.new { Fyi.call("New signup", body: @user.email, plan: @user.plan) }
end
endOr use an ActiveJob:
# app/jobs/notify_job.rb
class NotifyJob < ApplicationJob
queue_as :default
def perform(title, **opts)
Fyi.call(title, **opts)
end
end
# In your controller
NotifyJob.perform_later("New signup", body: @user.email, plan: @user.plan)In a Rake task or script
#!/usr/bin/env ruby
require "trigger_fyi"
start = Time.now
begin
result = run_backup
elapsed = (Time.now - start).round
Fyi.call("Backup complete", body: "#{result[:items]} items · #{elapsed}s")
rescue => e
Fyi.critical("Backup failed", body: e.message)
exit 1
endIn Sidekiq
class ReportWorker
include Sidekiq::Worker
def perform(user_id)
start = Time.now
result = ReportGenerator.run(user_id)
elapsed = (Time.now - start).round
Fyi.call("Report complete",
body: "#{result[:rows]} rows · #{elapsed}s",
user_id: user_id.to_s
)
rescue => e
Fyi.critical("Report failed",
body: e.message,
user_id: user_id.to_s
)
raise
end
endraise after Fyi.critical lets Sidekiq handle retries normally.
In Sinatra
require "sinatra"
require "trigger_fyi"
post "/webhooks/stripe" do
event = JSON.parse(request.body.read)
if event["type"] == "payment_intent.succeeded"
amount = event.dig("data", "object", "amount").to_f / 100
email = event.dig("data", "object", "receipt_email")
Thread.new { Fyi.call("Payment: $#{"%.2f" % amount}", body: email) }
end
"ok"
endSetup
npx trigger.fyiGenerates a key, subscribes your device. Then:
export TRIGGER_FYI_SECRET_KEY=your_key_hereOr add to your .env file (dotenv gem), Heroku config vars, or Rails credentials.
Related: Push notifications from Python · Push notifications from PHP · GitHub Actions notifications · What is trigger.fyi?