Back to Documentation

Ruby SDK Examples

Real-world examples showing how to use the OddSockets Ruby SDK in production applications

Download Example Code
Basic Usage
Available
Simple example showing how to connect, subscribe to a channel, and publish messages using the core SDK functionality with Ruby idioms.
require 'oddsockets'

client = OddSockets::Client.new(
  api_key: 'ak_live_1234567890abcdef'
)

channel = client.channel('my-channel')

# Subscribe to messages
channel.subscribe do |message|
  puts "Received: #{message}"
end

# Publish a message
channel.publish('Hello, Ruby!')
Rails Integration
Available
Complete Rails integration example showing initializers, controllers, and real-time messaging in a Rails application.
# config/initializers/oddsockets.rb
OddSockets.configure do |config|
  config.manager_url = Rails.application.credentials.oddsockets_url
  config.log_level = Rails.env.production? ? :info : :debug
end

# app/controllers/messages_controller.rb
class MessagesController < ApplicationController
  def create
    client = OddSockets::Client.new(api_key: current_user.api_key)
    channel = client.channel("user_#{current_user.id}")
    
    channel.publish(message_params[:content])
    render json: { status: 'sent' }
  end
end
Promise-like Async
Available
Demonstrates async operations using Concurrent::Promises for clean asynchronous programming in Ruby.
require 'oddsockets'

client = OddSockets::Client.new(api_key: 'ak_live_1234567890abcdef')
channel = client.channel('async-channel')

# Async operations with promises
channel.subscribe { |msg| puts msg }.wait
result = channel.publish('Hello, Async!').wait
history = channel.history(count: 10).wait

puts "Published: #{result['messageId']}"
puts "History: #{history.length} messages"
Thread Safety
Available
Shows how the SDK handles concurrent operations safely using concurrent-ruby collections and thread-safe patterns.
require 'oddsockets'

client = OddSockets::Client.new(api_key: 'ak_live_1234567890abcdef')
channel = client.channel('thread-safe-channel')

# Multiple threads can safely use the same client
threads = 10.times.map do |i|
  Thread.new do
    channel.publish("Message from thread #{i}")
  end
end

# Wait for all threads to complete
threads.each(&:join)

puts "All messages sent safely!"
Bulk Publishing
Available
Efficient bulk message publishing for high-throughput scenarios with error handling and result tracking.
require 'oddsockets'

client = OddSockets::Client.new(api_key: 'ak_live_1234567890abcdef')

# Bulk publish messages
bulk_messages = [
  { channel: "notifications", message: "System update" },
  { channel: "alerts", message: "High CPU usage" },
  { channel: "logs", message: "User login" }
]

results = client.publish_bulk(bulk_messages)

results.each_with_index do |result, index|
  if result[:success]
    puts "✅ Message #{index + 1} published"
  else
    puts "❌ Message #{index + 1} failed: #{result[:error]}"
  end
end
Global Configuration
Available
Shows how to configure the SDK globally and create clients with shared configuration settings.
require 'oddsockets'

# Configure globally
OddSockets.configure do |config|
  config.manager_url = "https://manager.oddsockets.com"
  config.timeout = 15
  config.log_level = :info
end

# Create client using global configuration
client = OddSockets.client("ak_your_api_key_here", 
                          user_id: "configured_user")

puts "✅ Client created with global configuration"
puts "   Manager URL: #{OddSockets.configuration.manager_url}"
puts "   Timeout: #{OddSockets.configuration.timeout}s"