Using Relay with Ruby on Rails
Connect your Rails app to Relay as a Pusher-compatible broadcaster.
Skip the server setup. Relay Cloud gives you managed WebSockets with a free tier. Connect in 60 seconds.
Try free →
Prerequisites
- A running Relay server
- Your App Key, Secret, and ID
1
Add Gem
Add the Pusher gem to your Gemfile:
gem 'pusher'
Then install:
bundle install
2
Configure the Initializer
Create config/initializers/relay.rb:
require 'pusher'
Pusher.app_id = ENV['RELAY_APP_ID']
Pusher.key = ENV['RELAY_APP_KEY']
Pusher.secret = ENV['RELAY_APP_SECRET']
Pusher.host = ENV.fetch('RELAY_HOST', 'ws.relaycloud.dev')
Pusher.port = ENV.fetch('RELAY_PORT', '443').to_i
Pusher.scheme = ENV.fetch('RELAY_SCHEME', 'https')
3
Environment Variables
RELAY_APP_ID=your-app-id
RELAY_APP_KEY=your-app-key
RELAY_APP_SECRET=your-app-secret
RELAY_HOST=ws.relaycloud.dev
4
Trigger an Event
Pusher.trigger('public-feed', 'message.sent', { message: 'Hello from Rails!' })
5
Controller Example
class MessagesController < ApplicationController
def create
@message = Message.create!(message_params)
Pusher.trigger('public-feed', 'message.sent', {
message: @message.body,
user_id: current_user.id
})
render json: @message
end
end
6
Frontend
On the client side, use the Pusher JS client to subscribe to channels and receive events in real time. See the Next.js guide for a full frontend example.
Relay implements the full Pusher HTTP API. Any gem or library that supports custom Pusher hosts works with Relay.
Ready to skip the server setup? Relay Cloud gives you a production WebSocket server instantly. Start free, upgrade when you grow.
Start free →