Real-world examples showing how to use the OddSockets Ruby SDK in production applications
Download Example Coderequire '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!')
# 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
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"
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!"
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
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"