Ruby
Publish events, sign channel and user auth, validate webhooks, read history, manage versioned messages, and run push workflows from Rails or any Rack app with the sockudo gem.
The sockudo gem is the official Ruby server SDK for Sockudo. It is a trusted backend library:
it holds your app secret, signs every HTTP API request, and produces the signatures your realtime
clients need to join private and presence channels. Use it from Rails, Sinatra, or any Rack app,
and from background jobs.
What it does in Ruby:
- Signed HTTP publishing to one channel, many channels, or a batch, with idempotency keys and a built-in retry for network failures.
- Private, presence, and encrypted channel authorization, user authentication, and user connection termination.
- Webhook signature validation from a
Rack::Request, including validation against extra key pairs during rotation. - Channel state, durable history, presence history, and presence snapshots.
- Versioned message reads and mutations (get, versions, update, delete, append) and annotations.
- Push notifications: device registrations, channel push subscriptions, publish, schedule, status, provider credentials, and Apple Live Activity broadcast channels.
- Optional non-blocking requests through EventMachine or
httpclientthreads.
Install
Add the gem to your Gemfile and install it:
gem "sockudo", "~> 2.2"bundle installThe current gem version is 2.2.0. The gemspec declares required_ruby_version >= 2.6; the SDK
README lists Ruby 3.0 or later as the supported baseline, so target a maintained Ruby 3.x. Runtime
dependencies are httpclient, multi_json, logger, and pusher-signature.
To work against the SDK source in a checkout of the Sockudo monorepo, point Bundler at the package path instead:
gem "sockudo", path: "../sockudo/server-sdks/sockudo-http-ruby"Two optional gems unlock extra features. Add rbnacl (which needs libsodium) for end-to-end
encrypted channels, and em-http-request for EventMachine-based async requests.
Configure the client
Create one client in an initializer and reuse it everywhere:
require "sockudo"
SOCKUDO = Sockudo::Client.new( app_id: ENV.fetch("SOCKUDO_APP_ID"), key: ENV.fetch("SOCKUDO_APP_KEY"), secret: ENV.fetch("SOCKUDO_APP_SECRET"), host: ENV.fetch("SOCKUDO_HOST", "127.0.0.1"), port: Integer(ENV.fetch("SOCKUDO_PORT", "6001")), use_tls: ENV.fetch("SOCKUDO_USE_TLS", "true") == "true").tap do |client| # Set timeouts before the first request: the HTTP client is built once and memoized. client.timeout = 5end| Option | Type | Default | Description |
|---|---|---|---|
app_id | String | — | Sockudo app ID. Required for every API call. |
key | String | — | App key. Sent with every signed request and embedded in auth signatures. |
secret | String | — | App secret. Signs API requests, channel auth, and user auth. |
host | String | api-mt1.sockudo.com | HTTP API host without a scheme. The default is a legacy hosted value; always set it for a self-hosted server. |
port | Integer | 443, or 80 when use_tls: false | HTTP API port. An explicit port always wins over the use_tls default. |
use_tls | Boolean | true | Use HTTPS. Set false only on a trusted local network. |
cluster | String | — | Builds api-CLUSTER.sockudo.com when host is not set. Not needed for self-hosted servers. |
encryption_master_key_base64 | String | — | Base64 of a 32-byte key for private-encrypted-* channels. |
http_proxy | String | — | Outbound proxy URL, for example http://user:pass@proxy:8080. |
auto_idempotency_key | Boolean | true | Generate an idempotency key for trigger and trigger_batch calls that do not pass one. |
encrypted | Boolean | — | Deprecated alias for use_tls; prints a deprecation warning. |
Timeouts are set with writers after construction, in seconds:
| Writer | Default | Description |
|---|---|---|
timeout= | — | Sets connect, send, and receive timeouts to the same value. |
connect_timeout= | 5 | TCP and TLS connect timeout. |
send_timeout= | 5 | Request write timeout. |
receive_timeout= | 5 | Response read timeout. |
keep_alive_timeout= | 30 | Idle keep-alive connection lifetime. |
You can also build a client from a connection URL:
# SOCKUDO_URL=https://APP_KEY:APP_SECRET@realtime.example.com:443/apps/APP_IDSOCKUDO = Sockudo::Client.from_env # reads SOCKUDO_URL; raises Sockudo::ConfigurationError if unset
# Or parse any variable or string you choose:# SOCKUDO = Sockudo::Client.from_url(ENV.fetch("REALTIME_URL"))Module-level calls such as Sockudo.trigger use a default client that is built from SOCKUDO_URL
when it is set. An explicit client is easier to reason about and to test.
Publish an event
response = SOCKUDO.trigger( "private-orders-42", "order.created", { id: "ord_123", total_cents: 4200 }, { socket_id: params[:socket_id] })trigger sends POST /apps/APP_ID/events and returns the parsed JSON response as a Hash with
symbol keys. The response is {} unless you request channel attributes with info: (for example
info: "user_count" on presence channels), in which case it contains :channels. Non-String data
is JSON-encoded with MultiJson; Strings are sent unchanged.
Pass socket_id when the publish comes from a user action that already updated the originating
browser tab. The server skips that one connection, so the user does not receive an echo of their
own change; every other subscriber still gets the event.
Asynchronous publishing
trigger_async, trigger_batch_async, get_async, and post_async avoid blocking the calling
thread. When an EventMachine reactor is running (and em-http-request is installed), they return
an EM::DefaultDeferrable; otherwise they return an HTTPClient::Connection immediately and run
the request on an httpclient thread.
EM.run do SOCKUDO.trigger_async("orders", "order.created", { id: "ord_123" }) .callback { |response| EM.stop } .errback { |error| warn("publish failed: #{error.class}"); EM.stop }endThe async variants do not use the built-in retry. An async call is not a durable queue: use a job for work that must survive a process restart.
Publish to many channels or in a batch
Pass an array of channel names to publish the same event to several channels in one request:
SOCKUDO.trigger( ["tenant-42-orders", "user-7-orders"], "order.updated", { id: "ord_123", status: "paid" }, { idempotency_key: "order.updated:tenant-42:ord_123:v3" })Use trigger_batch to send different events in one request. Each event is a Hash with
:channel, :name, :data, and optional :socket_id, :info, and :idempotency_key:
SOCKUDO.trigger_batch([ { channel: "orders", name: "order.created", data: { id: "ord_124" }, idempotency_key: "order.created:ord_124" }, { channel: "private-user-7", name: "order.receipt", data: { id: "ord_124" }, idempotency_key: "order.receipt:ord_124" }])The SDK raises Sockudo::Error before sending when a single trigger names more than 100
channels, or when a multi-channel publish includes a private-encrypted-* channel. It does not
cap batch size or payload size locally. The server enforces its [event_limits] defaults:
| Limit | Default |
|---|---|
max_channels_at_once | 100 channels per publish |
max_batch_size | 10 events per batch |
max_payload_in_kb | 100 KB per event payload |
max_name_length | 200 characters per event or channel name |
Idempotency and retries
A timeout does not tell you whether the server published the event. If you retry without a key,
subscribers can receive the event twice. An idempotency key makes the retry safe: within the TTL,
the server returns the original cached response (200) instead of publishing again.
Server semantics:
- The key can be sent as the body field
idempotency_keyor theX-Idempotency-Keyheader. The body field takes precedence. [idempotency]defaults areenabled = true,ttl_seconds = 120, andmax_key_length = 128. Empty keys are rejected.- In a batch,
X-Idempotency-Keyidentifies the whole batch and each event’sidempotency_keyidentifies only that event. The two key domains do not collide. - If the coordination backend is unavailable, the server returns
503 service_unavailable. A concurrent retry while the first attempt is still in flight gets503 backpressurewithRetry-After: 1. Retry both with the same key.
How the Ruby SDK applies keys:
- Pass
idempotency_key:in the options Hash oftrigger. The SDK sends it as both the body field and theX-Idempotency-Keyheader. - In
trigger_batch, set:idempotency_keyon each event. The SDK does not send a batch-level header. - With
auto_idempotency_key: true(the default), calls without a key get a generated one in the formBASE:COUNTER(andBASE:COUNTER:INDEXfor batch events), whereBASEis random per client instance. That key protects only the SDK’s own retry of the same call; a new call, process, or job attempt gets a new key. triggerandtrigger_batchretrySockudo::HTTPError(connection refused, timeouts, socket errors, malformed responses) up to 3 attempts in total, immediately and with the same body and key. HTTP error responses, including429and5xx, are raised without a retry.
For retries you control, such as Active Job retries, derive the key from the logical operation and pass it explicitly so every attempt shares it:
class PublishOrderPaidJob < ApplicationJob retry_on Sockudo::HTTPError, wait: 5.seconds, attempts: 5
def perform(order_id, version) SOCKUDO.trigger( "private-orders-#{order_id}", "order.paid", { id: order_id, version: version }, { idempotency_key: "order.paid:#{order_id}:v#{version}" } ) endendSockudo.generate_idempotency_key returns a random UUID. If you use it, generate it once when you
enqueue the work and pass it to every attempt.
Authorize private and presence channels
When a client subscribes to a private-* or presence-* channel, it asks your backend for a
signature. Your endpoint decides whether the current user may join; the SDK only signs the answer.
For presence channels, take user_id and user_info from your own session, never from request
parameters, or any user could claim to be anyone.
post "/sockudo/auth", to: "sockudo_auth#create"post "/sockudo/user-auth", to: "sockudo_auth#user"class SockudoAuthController < ApplicationController # Keep CSRF protection on: the client should send the CSRF token with its auth request. before_action :require_login
def create channel_name = params.require(:channel_name) socket_id = params.require(:socket_id)
return head :forbidden unless can_subscribe?(current_user, channel_name)
auth = if channel_name.start_with?("presence-") SOCKUDO.authenticate( channel_name, socket_id, user_id: current_user.id.to_s, user_info: { name: current_user.display_name } ) else SOCKUDO.authenticate(channel_name, socket_id) end
render json: auth rescue Sockudo::Error # Invalid socket_id or channel name. head :bad_request end
private
def can_subscribe?(user, channel_name) # Replace with your own rules, for example tenant or ownership checks. channel_name.end_with?("-user-#{user.id}") || user.member_of_channel?(channel_name) endendThe client sends socket_id and channel_name form-encoded. authenticate validates the socket ID
format (123.456) and the channel name (letters, digits, and _-=@,.;:, at most 200 characters),
then returns a Hash: { auth: "KEY:SIGNATURE" } for private channels, plus channel_data for
presence channels and shared_secret for encrypted channels.
User authentication
User authentication (sign-in) binds a connection to a user ID so the server can target that user and terminate their connections:
def user render json: SOCKUDO.authenticate_user( params.require(:socket_id), { id: current_user.id.to_s, name: current_user.display_name } )enduser_data must be a Hash with a non-empty String under the symbol key :id; otherwise
authenticate_user raises Sockudo::Error. It returns { auth: ..., user_data: ... }.
Encrypted channels
private-encrypted-* channels are end-to-end encrypted: the server relays ciphertext it cannot
read. Configure a 32-byte master key, encoded as base64, and add rbnacl to your Gemfile:
openssl rand -base64 32SOCKUDO = Sockudo::Client.new( app_id: ENV.fetch("SOCKUDO_APP_ID"), key: ENV.fetch("SOCKUDO_APP_KEY"), secret: ENV.fetch("SOCKUDO_APP_SECRET"), host: ENV.fetch("SOCKUDO_HOST"), encryption_master_key_base64: ENV.fetch("SOCKUDO_ENCRYPTION_MASTER_KEY"))The SDK encrypts event data with a per-channel key derived from the master key and adds that
channel’s shared_secret to the auth response. Publish to one encrypted channel per call:
multi-channel publishes that include an encrypted channel raise Sockudo::Error. Batches may
contain encrypted events.
Terminate user connections
When access changes, disconnect a user’s sockets immediately:
SOCKUDO.terminate_user_connections(user.id.to_s)
# Or close every socket with code 4200 so clients reconnect and re-authenticate:SOCKUDO.force_reconnect_user(user.id.to_s)Validate webhooks
Sockudo signs each webhook with HMAC-SHA256 over the exact request body bytes and sends the result
in X-Pusher-Signature, with the app key in X-Pusher-Key. Validation must see the raw body:
parsing and re-serializing the JSON changes the bytes and breaks the signature. Pass the original
Rack::Request or ActionDispatch::Request; the SDK reads and rewinds the raw body itself.
class SockudoWebhooksController < ActionController::Base skip_forgery_protection
def create webhook = SOCKUDO.webhook(request) return head :unauthorized unless webhook.valid?
# Acknowledge fast; do the work in a job. SockudoWebhookJob.perform_later(webhook.events) head :ok endendwebhook.events is an Array of Hashes with String keys ("name", "channel", "event",
"data", "socket_id", "user_id", and future fields), and webhook.time is the event time.
Always call valid? first: events does not check the signature. The body is parsed only when its
Content-Type is application/json, which is what Sockudo sends.
Sockudo may deliver the same webhook more than once, so make the job idempotent, for example by
recording processed event identities. During key rotation, pass the other key pairs to valid?:
previous = { key: ENV["SOCKUDO_PREVIOUS_KEY"], secret: ENV["SOCKUDO_PREVIOUS_SECRET"] }webhook.valid?([previous])The extra pairs are tried only when the X-Pusher-Key header does not match the client’s key.
Channel state and history
channels = SOCKUDO.channels(filter_by_prefix: "presence-", info: "user_count")info = SOCKUDO.channel_info("presence-lobby", info: "user_count,subscription_count")users = SOCKUDO.channel_users("presence-lobby")[:users] # [{ id: "user-42" }, ...]State is an operational snapshot at the moment of the request; never use it as an authorization source.
Durable history returns pages of stored messages. Supported parameters are :limit,
:direction ("newest_first" or "oldest_first"), :cursor, :start_serial, :end_serial,
:start_time_ms, and :end_time_ms:
cursor = nilloop do page = SOCKUDO.channel_history( "orders", { limit: 100, direction: "oldest_first", cursor: cursor }.compact ) page[:items].each { |item| export(item) } break unless page[:has_more]
cursor = page[:next_cursor]endCursors are opaque: pass next_cursor back unchanged and do not parse or construct them. The
server caps page sizes, so keep limit bounded.
Presence history records join and leave transitions on presence channels, and a snapshot rebuilds membership at a serial or timestamp:
transitions = SOCKUDO.channel_presence_history("presence-lobby", limit: 50, direction: "newest_first")members = SOCKUDO.channel_presence_snapshot("presence-lobby", at_serial: 4) # or at_time_ms:The same calls are available on a channel object, for example
SOCKUDO["orders"].history(limit: 50) and SOCKUDO["presence-lobby"].presence_history.
For endpoints without a dedicated method, the signed request helpers get, post, and delete
take a path relative to /apps/APP_ID and handle signing:
SOCKUDO.get("/channels", filter_by_prefix: "private-")Versioned messages and annotations
Protocol V2 messages can be read and mutated after publish. The SDK wraps the HTTP surface described in Mutable messages; request bodies are passed through as Hashes:
latest = SOCKUDO.get_message("chat-room-1", message_serial)versions = SOCKUDO.get_message_versions("chat-room-1", message_serial, limit: 20)
SOCKUDO.update_message("chat-room-1", message_serial, { data: "hello, edited", description: "fix typo", op_id: "edit:#{message_serial}:2"})SOCKUDO.append_message("chat-room-1", message_serial, { data: " more text" })SOCKUDO.delete_message("chat-room-1", message_serial, { description: "removed by moderator" })Mutation responses include :message_serial, :action, :accepted, :status, and the new
:version_serial. op_id makes a mutation idempotent: replaying the same ID with the same
operation returns the original acknowledgement. Without socket_id, a signed app-key request is a
privileged server operation.
Annotations attach typed reactions or flags to a message:
created = SOCKUDO.publish_annotation("chat-room-1", message_serial, { type: "reactions:distinct.v1", name: "thumbs_up", clientId: "user-42"})events = SOCKUDO.list_annotations("chat-room-1", message_serial, type: "reactions:distinct.v1")SOCKUDO.delete_annotation("chat-room-1", message_serial, created[:annotationSerial])Push notifications
The push helpers sign /apps/APP_ID/push/* requests and send the push capability header for you
(push-admin by default, push-subscribe when you pass a device identity token). Request bodies
are passed through as Hashes in the server’s camelCase shape. See
Push notifications for the full model.
Register a device from your backend after you verify the user:
device_id = params.require(:installation_id)
registration = SOCKUDO.activate_device({ appId: ENV.fetch("SOCKUDO_APP_ID"), id: device_id, clientId: current_user.id.to_s, formFactor: "phone", platform: "ios", deviceSecret: "admin-registration-placeholder", timezone: "Europe/Bucharest", locale: "en", push: { recipient: { transportType: "apns", deviceToken: params.require(:apns_token) }, state: "ACTIVE" }})
device_identity_token = registration[:deviceIdentityToken]With admin capability the server ignores the deviceSecret value and issues a device identity
token on first registration; pass rotate_device_identity_token: true as the second argument to
issue a new one. Store the token for device-scoped calls such as update_device_registration.
Subscribe the device to a channel, publish, and check status:
SOCKUDO.upsert_channel_push_subscription({ appId: ENV.fetch("SOCKUDO_APP_ID"), channel: "private-orders-42", deviceId: device_id, clientId: current_user.id.to_s, provider: "apns", tokenHash: registration[:tokenHash]})
accepted = SOCKUDO.publish_push({ publishId: "push:order-ord_123:packed", recipients: [{ type: "channel", channel: "private-orders-42" }], payload: { title: "Order updated", body: "Order ord_123 is packed" }})
status = SOCKUDO.get_publish_status(accepted[:publishId])publish_push always sends sync: false. The 202 response is an admission record
(publishId, status, expectedRecipients), not proof of delivery; inspect delivery
asynchronously with get_publish_status. Reuse the same publishId when you retry a publish.
Other push methods:
| Area | Methods |
|---|---|
| Devices | activate_device (alias create_device_activation), update_device_registration, get_device_registration, list_device_registrations, delete_device_registration, remove_device_registrations_by_client |
| Subscriptions | upsert_channel_push_subscription, list_channel_push_subscriptions, delete_channel_push_subscriptions, list_channel_push_subscription_channels |
| Publishing | publish_push (alias publish_push_direct), publish_push_batch, schedule_push (requires notBeforeMs), cancel_scheduled_push, get_publish_status, post_push_delivery_status |
| Credentials | put_push_credential, list_push_credentials |
For Apple Live Activities, manage iOS 18 broadcast channels with
create_apns_live_activity_channel (storage policy "noStorage" or "mostRecent"),
get_apns_live_activity_channel, list_apns_live_activity_channels, and
delete_apns_live_activity_channel, and publish direct or broadcast ActivityKit payloads with
publish_push. See Apple Live Activities for the wire-safe
request shapes.
Errors and timeouts
Every SDK error descends from Sockudo::Error:
| Error | Raised when |
|---|---|
Sockudo::AuthenticationError | The server returns 401. Fix credentials or clock skew; do not retry unchanged. |
Sockudo::ConfigurationError | app_id, key, secret, or a required URL variable is missing. |
Sockudo::HTTPError | The HTTP client failed: connection refused, timeout, socket error, or malformed response. original_error holds the underlying exception. Other low-level exceptions, such as TLS errors, are not wrapped. |
Sockudo::Error | Any other non-success status, plus local validation failures. |
The SDK does not expose a status attribute; non-success statuses other than 401 are reported in
the message, for example Bad request: ... for 400 or Unknown error (status code 503): ... for
unmapped codes. A 413 is reported as Payload Too Large > 10KB, but the limit that applies is
the server’s max_payload_in_kb (100 KB by default).
Retry network errors, 429, and 5xx with bounded backoff and the original idempotency key. Do
not retry 400, 401, 403, or 413: the same request fails the same way.
RETRYABLE_STATUS = /status code (429|5\d\d)/
begin SOCKUDO.trigger("orders", "order.created", payload, { idempotency_key: operation_id })rescue Sockudo::AuthenticationError raise # Fix credentials or clock skew; retrying will not help.rescue Sockudo::HTTPError PublishRetryJob.set(wait: 5.seconds).perform_later(operation_id)rescue Sockudo::Error => error raise unless error.message.match?(RETRYABLE_STATUS)
PublishRetryJob.set(wait: 5.seconds).perform_later(operation_id)endKeep the SDK timeout shorter than the web request or job deadline that wraps it; the defaults are 5 seconds each for connect, send, and receive.