How to use Ably
There are several different ways to use Ably in an application. These include using an Ably SDK, choosing a protocol adapter for libraries such as MQTT or SSE, or utilizing an integration to communicate between Ably and external systems. Each method provides slightly different benefits and feature sets. This page discusses the options available.
Ably SDKs provide a consistent and idiomatic API across a variety of supported platforms and are the most feature-rich method of integrating Ably into an application. Ably SDKs contain a realtime and a REST interface, each of which can be used to satisfy different use cases.
REST interface
The REST interface communicates with the Ably service using the HTTP protocol and is effectively stateless. The REST interface provides a convenient way to access the REST HTTP API and is intended to be used by clients that don’t require realtime updates. It is more commonly used server-side. It is used to:
- publish messages
- publish messages on behalf of other clients
- issue tokens on behalf of other realtime clients
- retrieve persisted messages, presence history and application usage statistics
Realtime interface
The realtime interface is implemented using an Ably-defined protocol, primarily over WebSockets. It enables clients to establish and maintain a persistent connection to Ably. The realtime interface extends the functionality of the REST interface and is most commonly used client-side. It is used to:
- maintain a persistent connection to Ably
- attach to one or more channels, and publish and subscribe to messages to them
- register their presence on a channel, or listen for others present in realtime
- publish at very high message rates, or with the lowest possible realtime latencies
REST HTTP API
Interacting with the REST HTTP API directly is fully supported. However, Ably recommends using the REST interface of an SDK where possible, as they provide additional features that improve performance and resilience that the REST HTTP API can’t deliver on its own. This includes automatic re-routing around network problems by using alternative data centers.
Protocol adapters
Protocol adapters are an alternative method of using Ably and provide interoperability between Ably’s protocol and other popular protocols, such as MQTT. They are less feature-rich than Ably SDKs, however they require fewer resources in terms of memory and network overhead to run. Protocol adapters can also be used in applications built using platforms that don’t have an available Ably SDK.
The following protocol adapters are available:
MQTT
The Ably MQTT protocol adapter translates between the Message Queuing Telemetry Transport (MQTT) and Ably’s own protocol. MQTT is a lightweight messaging protocol optimized for high-latency and unreliable networks. It is often used in remote devices with small footprints. MQTT libraries are available for most internet-enabled devices.
You can use the MQTT protocol adapter with your MQTT library when:
- an Ably SDK isn’t available for your target platform
- bandwidth is limited and you want to keep network traffic to a minimum
- you want to avoid any type of vendor lock-in by using open protocols
Ably SSE and EventSource API
The Ably SSE (Server Sent Events) and raw HTTP streaming API provides a way to get a realtime stream of events from Ably when using a full Ably SDK is impractical. HTTP streaming enables a client’s requests to be held by a server, enabling it to push data to the client without further requests. Note that it isn’t possible to publish using SSE. It is only possible to subscribe to messages and events.
You can use the SEE protocol adapter when:
- an Ably SDK isn’t available for your target platform
- you have stringent memory restrictions
- you only need to subscribe to events on channels, not publish them
Pusher and PubNub
Ably provides protocol adapters for Pusher and PubNub. These protocol adapters require minimal changes in your code to initially switch over to using Ably. They enable you to migrate from Pusher or PubNub incrementally, or all at once.
Ably Asset Tracking
Ably Asset Tracking provides a way to track the location of multiple assets in realtime. An asset is any object that moves geographically, and whose position needs to be monitored.
Asset tracking is powered by the Ably realtime network, Ably Asset Tracking SDKs, and the Mapbox Navigation SDK with location enhancement. Ably Asset Tracking SDKs are available for iOS, Android, and web clients.
Integrations
Integrations enable data to be sent between Ably channels and external services. Inbound integrations are where data is sent from an external service into Ably. Outbound integrations are where data is sent from Ably to an external service.
Inbound integrations
Inbound integrations enable you to publish data from an external web service to an Ably channel. Examples include IFTTT and Zapier.
Outbound integrations
Outbound integrations enable you to send data from Ably channels to an external streaming or queuing service. Examples include Amazon Kinesis or Amazon SQS.