• Stars
    star
    34
  • Rank 740,949 (Top 16 %)
  • Language
    Ruby
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ruby client library SDK for Ably realtime messaging service

Features

Gem Version Coverage Status

Ably is the platform that powers synchronized digital experiences in realtime. Whether attending an event in a virtual venue, receiving realtime financial information, or monitoring live car performance data – consumers simply expect realtime digital experiences as standard. Ably provides a suite of APIs to build, extend, and deliver powerful digital experiences in realtime for more than 250 million devices across 80 countries each month. Organizations like Bloomberg, HubSpot, Verizon, and Hopin depend on Ably’s platform to offload the growing complexity of business-critical realtime data synchronization at global scale. For more information, see the Ably documentation.

This is a Ruby client library for Ably. The library currently targets the Ably 1.2 client library specification. You can see the complete list of features this client library supports in our client library SDKs feature support matrix.

Supported platforms

This SDK supports Ruby 2.7 and 3.x. For eventmachine and Ruby 3.x note please visit Ruby 3.0 support section.

As of v1.1.5 this library requires libcurl as a system dependency. On most systems this is already installed but in rare cases where it isn't (for example debian-slim Docker images such as ruby-slim) you will need to install it yourself. On debian you can install it with the command sudo apt-get install libcurl4.

We regression-test the SDK against a selection of Ruby versions (which we update over time, but usually consists of mainstream and widely used versions). Please refer to .github/workflows/check.yml for the set of versions that currently undergo CI testing.

If you find any compatibility issues, please do raise an issue in this repository or contact Ably customer support for advice.

Documentation

Visit https://ably.com/documentation for a complete API reference and code examples.

Installation

The client library is available as a gem from RubyGems.org.

Add this line to your application's Gemfile:

gem 'ably'

And then install this Bundler dependency:

$ bundle

Or install it yourself as:

$ gem install ably

Using with Rails or Sinatra

This ably gem provides both a Realtime and REST version of the Ably library. Realtime depends on EventMachine to provide an asynchronous evented framework to run the library in, whereas the REST library depends only on synchronous libraries such as Faraday.

If you are using Ably within your Rails or Sinatra apps, more often than not, you probably want to use the REST only version of the library that has no dependency on EventMachine and provides a synchronous API that you will be used to using within Rails and Sinatra. See the REST only Ruby version of the Ably library.

Using the Realtime API

Introduction

All examples must be run within an EventMachine reactor as follows:

EventMachine.run do
  # ...
end

All examples assume a client has been created using one of the following:

# basic auth with an API key
client = Ably::Realtime.new(key: 'xxxxx')

# using token auth
client = Ably::Realtime.new(token: 'xxxxx')

If you do not have an API key, sign up for a free API key now

Connection

Successful connection:

client.connection.connect do
  # successful connection
end

Failed connection:

connection_result = client.connection.connect
connection_result.errback = Proc.new do
  # failed connection
end

Subscribing to connection state changes:

client.connection.on do |state_change|
  state_change.current #=> :connected
  state_change.previous #=> :connecting
end

Subscribing to a channel

Given a channel is created as follows:

channel = client.channels.get('test')

Subscribe to all events:

channel.subscribe do |message|
  message.name #=> "greeting"
  message.data #=> "Hello World!"
end

Only certain events:

channel.subscribe('myEvent') do |message|
  message.name #=> "myEvent"
  message.data #=> "myData"
end

Publishing a message to a channel

channel.publish('greeting', 'Hello World!')

Querying the History

channel.history do |messages_page|
  messages_page #=> #<Ably::Models::PaginatedResult ...>
  messages_page.items.first # #<Ably::Models::Message ...>
  messages_page.items.first.data # payload for the message
  messages_page.items.length # number of messages in the current page of history
  messages_page.next do |next_page|
    next_page #=> the next page => #<Ably::Models::PaginatedResult ...>
  end
  messages_page.has_next? # false, there are more pages
end

Presence on a channel

channel.presence.enter(data: 'metadata') do |presence|
  presence.get do |members|
    members #=> [Array of members present]
  end
end

Subscribing to presence events

channel.presence.subscribe do |member|
  member #=> { action: :enter, client_id: 'bob' }
end

Querying the Presence History

channel.presence.history do |presence_page|
  presence_page.items.first.action # Any of :enter, :update or :leave
  presence_page.items.first.client_id # client ID of member
  presence_page.items.first.data # optional data payload of member
  presence_page.next do |next_page|
    next_page #=> the next page => #<Ably::Models::PaginatedResult ...>
  end
  presence_page.has_next? # false, there are more pages
end

Symmetric end-to-end encrypted payloads on a channel

When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.

secret_key = Ably::Util::Crypto.generate_random_key
channel = client.channels.get('test', cipher: { key: secret_key })
channel.subscribe do |message|
  message.data #=> "sensitive data (encrypted before being published)"
end
channel.publish "name (not encrypted)", "sensitive data (encrypted before being published)"

Using the REST API

Introduction

Unlike the Realtime API, all calls are synchronous and are not run within EventMachine.

All examples assume a client and/or channel has been created as follows:

client = Ably::Rest.new(key: 'xxxxx')
channel = client.channel('test')

Publishing a message to a channel

channel.publish('myEvent', 'Hello!') #=> true

Querying the History

messages_page = channel.history #=> #<Ably::Models::PaginatedResult ...>
messages_page.items.first #=> #<Ably::Models::Message ...>
messages_page.items.first.data # payload for the message
messages_page.next # retrieves the next page => #<Ably::Models::PaginatedResult ...>
messages_page.has_next? # false, there are more pages

Current presence members on a channel

members_page = channel.presence.get # => #<Ably::Models::PaginatedResult ...>
members_page.items.first # first member present in this page => #<Ably::Models::PresenceMessage ...>
members_page.items.first.client_id # client ID of first member present
members_page.next # retrieves the next page => #<Ably::Models::PaginatedResult ...>
members_page.has_next? # false, there are more pages

Querying the presence history

presence_page = channel.presence.history #=> #<Ably::Models::PaginatedResult ...>
presence_page.items.first #=> #<Ably::Models::PresenceMessage ...>
presence_page.items.first.client_id # client ID of first member
presence_page.next # retrieves the next page => #<Ably::Models::PaginatedResult ...>

Symmetric end-to-end encrypted payloads on a channel

When a 128 bit or 256 bit key is provided to the library, all payloads are encrypted and decrypted automatically using that key on the channel. The secret key is never transmitted to Ably and thus it is the developer's responsibility to distribute a secret key to both publishers and subscribers.

secret_key = Ably::Util::Crypto.generate_random_key
channel = client.channels.get('test', cipher: { key: secret_key })
channel.publish nil, "sensitive data" # data will be encrypted before publish
messages_page = channel.history
messages_page.items.first.data #=> "sensitive data"

Generate a Token

Tokens are issued by Ably and are readily usable by any client to connect to Ably:

token_details = client.auth.request_token
# => #<Ably::Models::TokenDetails ...>
token_details.token # => "xVLyHw.CLchevH3hF....MDh9ZC_Q"
client = Ably::Rest.new(token: token_details)

Generate a TokenRequest

Token requests are issued by your servers and signed using your private API key. This is the preferred method of authentication as no secrets are ever shared, and the token request can be issued to trusted clients without communicating with Ably.

token_request = client.auth.create_token_request(ttl: 3600, client_id: 'jim')
# => {"id"=>...,
#     "clientId"=>"jim",
#     "ttl"=>3600,
#     "timestamp"=>...,
#     "capability"=>"{\"*\":[\"*\"]}",
#     "nonce"=>...,
#     "mac"=>...}

client = Ably::Rest.new(token: token_request)

Fetching your application's stats

stats_page = client.stats #=> #<Ably::Models::PaginatedResult ...>
stats_page.items.first = #<Ably::Models::Stats ...>
stats_page.next # retrieves the next page => #<Ably::Models::PaginatedResult ...>

Fetching the Ably service time

client.time #=> 2013-12-12 14:23:34 +0000

Ruby 3.0 support

If you cannot install ably realtime gem because of eventmachine openssl problems, please try to set your openssl-dir, i.e.:

gem install eventmachine -- --with-openssl-dir=/usr/local/opt/openssl@1.1

More about eventmachine and ruby 3.0 support here eventmachine/eventmachine#932

Dependencies

If you only need to use the REST features of this library and do not want EventMachine as a dependency, then you should consider using the Ably Ruby REST gem.

Upgrading from an older version

Support, feedback and troubleshooting

Please visit https://ably.com/support for access to our knowledgebase and to ask for any assistance.

You can also view the community reported Github issues.

To see what has changed in recent versions of Bundler, see the CHANGELOG.

Contributing

  1. Fork it
  2. When pulling to local, make sure to also pull the ably-common repo (git submodule init && git submodule update)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Ensure you have added suitable tests and the test suite is passing(bundle exec rspec)
  6. Push to the branch (git push origin my-new-feature)
  7. Create a new Pull Request

Release process

This library uses semantic versioning. For each release, the following needs to be done:

  • Update the version number in version.rb and commit the change.
  • Run github_changelog_generator to automate the update of the CHANGELOG. Once the CHANGELOG update has completed, manually change the Unreleased heading and link with the current version number such as v1.0.0. Also ensure that the Full Changelog link points to the new version tag instead of the HEAD. Ideally, run rake doc:spec to generate a new spec file. Then commit these changes.
  • Add a tag and push to origin such as git tag v1.0.0 && git push origin v1.0.0
  • Visit https://github.com/ably/ably-ruby/tags and Add release notes for the release including links to the changelog entry.
  • Run rake release to publish the gem to Rubygems
  • Release the REST-only library ably-ruby-rest

More Repositories

1

ably-js

Javascript, Node, Typescript, React, React Native client library SDK for Ably realtime messaging service
JavaScript
292
star
2

ably-java

Java, Android, Clojure and Scala client library SDK for Ably realtime messaging service
Java
79
star
3

ably-go

Go client library SDK for Ably realtime messaging service
Go
78
star
4

ably-flutter

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.
Dart
58
star
5

ably-python

Python client library SDK for Ably realtime messaging service
Python
46
star
6

ably-php

PHP client library SDK for Ably realtime messaging service
PHP
46
star
7

ably-dotnet

.NET, Xamarin, MAUI, Mono and Unity client library SDK for Ably realtime messaging service
C#
44
star
8

ably-cocoa

iOS, tvOS and macOS Objective-C and Swift client library SDK for Ably realtime messaging service
Swift
42
star
9

spaces

The Spaces SDK enables you build collaborative spaces for your application and enable features like Avatar stack, Live cursors, Member location, Component locking and more.
TypeScript
42
star
10

laravel-broadcaster

Official Laravel Ably Broadcaster
PHP
36
star
11

tutorials

Ably Tutorials in multiple languages
35
star
12

ably-php-laravel

PHP Laravel framework client library SDK for Ably realtime messaging service
PHP
26
star
13

docs

Ably Realtime API documentation
TypeScript
19
star
14

wiki-site

Convert your GitHub wiki into your own personalised site. Check more information on the choices made at https://blog.ably.io/hacking-github-to-build-your-own-wiki-ab08aab58db1
HTML
19
star
15

proxy-protocol-v2

A simple encoder and decoder for the proxy protocol v2 binary format
JavaScript
18
star
16

ably-asset-tracking-android

Android client SDKs for the Ably Asset Tracking service.
Kotlin
12
star
17

ably-rust

Rust SDK for Ably realtime messaging service. Developer Preview.
Rust
11
star
18

kafka-connect-ably

Kafka Connector for publishing data from Kafka to Ably
Java
11
star
19

repository-audit

Oversight for our estate of repositories, in particular those in the public domain. Audit. Monitor. Conform.
JavaScript
11
star
20

terraform-provider-ably

Ably's Terraform Provider, enabling you to manage your Ably account programmatically.
Go
10
star
21

ably-asset-tracking-swift

iOS client SDKs for the Ably Asset Tracking service.
Swift
9
star
22

ably-asset-tracking-js

JavaScript client SDKs for the Ably Asset Tracking service.
TypeScript
9
star
23

ably-js-react-native

React Native client library SDK for Ably realtime messaging service
JavaScript
8
star
24

ably-boomer

Ably load generator for locust
Go
7
star
25

ably-roku

Subscribing to Ably channels on the Roku platform using Brightscript.
Brightscript
6
star
26

ably-common

Common files shared by Ably realtime messaging libraries
JavaScript
6
star
27

ably-js-nativescript

NativeScript client library SDK for Ably realtime messaging service
JavaScript
5
star
28

open-specs

Descriptions for all Open Specifications available for Ably
HTML
5
star
29

ably-ruby-rest

Ruby REST client library SDK for Ably realtime messaging service
Ruby
5
star
30

features

The Ably canonical features list. SDK Team. Incubating.
JavaScript
5
star
31

design-patterns

Ably Design Patterns for it's most common use cases
4
star
32

demo-mobile-android

A simple mobile app built for Android using the ably-java realtime library
Java
4
star
33

demo-mobile-phonegap-cordova

A simple mobile app built for Phonegap / Cordova using ably-js realtime library
JavaScript
4
star
34

sdk-api-reference

Language-agnostic reference API commentaries for SDK developers to use when adding docstring comments to Ably SDKs.
3
star
35

delta-codec-dotnet

C#
3
star
36

demo-mobile-ios

A simple mobile app built for iOS using the ably-io realtime library
Swift
3
star
37

ably-asset-tracking-common

Common resources used by the Ably Asset Tracking SDKs.
JavaScript
3
star
38

protocol-demo

JavaScript
2
star
39

realtime-use-case-devrel-collaborations

TypeScript
2
star
40

delta-codec-js

JavaScript
2
star
41

push-example-ios

This app provides an interactive interface to test the various methods and actions available in Ably's Push Notifications product.
Swift
2
star
42

asset-tracking-backend-demo

Incubating.
JavaScript
2
star
43

quickstart-js

Contains source code for the JavaScript and Node versions of the Quickstart guide
JavaScript
2
star
44

engineering

Ably's new home for engineering guidance, including content previously served from Confluence at engineering.ably.com.
2
star
45

firehose-sqs-tutorial

JavaScript
1
star
46

actions

Ruby
1
star
47

kotlin

Incubating. SDK Team. Our home for pure Kotlin code, here at Ably. We plan to publish modules to Maven Central from sources contained here very soon.
1
star
48

jwt-auth-node-tutorial

JWT authentication with Node.js tutorial code
CSS
1
star
49

asset-tracking-android-customer-app-demo

Incubating.
Kotlin
1
star
50

delta-codec-cocoa

Cocoa VCDiff decoder
Objective-C++
1
star
51

giveitalob

A fun realtime demo - see how high you can throw your phone
JavaScript
1
star
52

queue-demo

Demonstration of Ably queue functionality
JavaScript
1
star
53

ably-ui

Home of the Ably design system library.
TypeScript
1
star
54

xdelta-async-nodejs-addon

Asynchronous native addon for Node.js for generation and application of patches in VCDIFF format using the Xdelta algorithm
C++
1
star
55

asset-tracking-web-customer-app-demo

Incubating.
TypeScript
1
star
56

asset-tracking-android-rider-app-demo

Incubating.
Kotlin
1
star
57

example-lambda-function

An example of an AWS Lambda function that you can invoke with a Ably Reactor Function rule, showing how to process that event and publish a message back to Ably from the same function
JavaScript
1
star
58

demo-json-patch

A simple demo to demonstrate using Ably pub/sub and history to build a JSON Patch service
HTML
1
star
59

delta-codec-java

Java
1
star
60

demo-sinatra-chat

Ruby Sinatra demonstration building a multi-user chat app
JavaScript
1
star