About Pub/Sub
Ably Pub/Sub is Ably's core product. Its flexible APIs are powerful building blocks that you can use to create any realtime experience with.
Features
The following features are some of the basics that you need to develop realtime applications:
Publish and subscribe
Ably Pub/Sub enables you to implement the publish-subscribe (pub-sub) pattern. Any number of publishers can send messages to a channel, and any number of subscribers can receive those messages. Publishers and subscribers are completely decoupled from one another.
Channels are used to separate messages into different topics. Messages contain the data that a client is communicating, such as the contents of an individual chat message, or an event that has occurred, such as updated financial information. Whilst billions of messages may be delivered by Ably, clients receive only the messages on the channels they subscribe to.
Presence
The presence feature enables clients to be aware of other clients that are currently "present" on a channel. Subscribers receive three types of updates from presence members. These are when a client joins the presence set, when they leave the presence set, and when they update an optional payload associated with each member. The payload can be used to describe their status, or attributes associated with them, such as setting their status to 'out for lunch'.
Presence is most commonly used as an online indicator to create an avatar stack for an application, or to notify occupants of a chat room that a member has joined or left.
Message history
Messages received by Ably are stored in memory for 2 minutes in every location that the channel is active in. This enables Pub/Sub SDKs to automatically retrieve them in the event of network connectivity issues, or a lost connection, as long as the connection is re-established within 2 minutes.
Messages can be stored for much longer on disk by Ably through additional configuration. The history feature can be used to retrieve previously sent messages as a paginated list. History can retrieve messages from as far back as the message persistence configured for a channel.
Push notifications
Push notifications notify user devices whether or not an application is open and running. They deliver information, such as app updates, social media alerts, or promotional offers, directly to the user's screen.
Ably sends push notifications to devices using Google's Firebase Cloud Messaging service (FCM) and Apple's Push Notification Service (APNs). Push notifications don't require a device to stay connected to Ably. Instead, a device's operating system maintains its own battery-efficient transport to receive notifications.
You can publish push notifications to user devices directly or via channels.
Device and server packages
Pub/Sub 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. Each Ably SDK provides a device package, a server package, or both. Use the respective package with each environment: device packages on the client-side and server packages on your servers and in your serverless infrastructure.
Device package
Device packages are intended for use in client-side applications, such as web browsers and mobile apps, and provide a realtime client.
Realtime client
A realtime client uses an Ably-defined protocol, primarily over WebSockets, to establish and maintain a persistent connection to Ably. Features that depend on that connection, such as subscribing to messages, presence events, channel options such as rewind and deltas, and connection state, need a realtime client.
Use a realtime client to:
- Attach to one or more channels to subscribe to them in realtime.
- Register presence on a channel, or listen for others' present in realtime.
- Publish at very high message rates, or with the lowest possible latencies.
- Benefit from automatic reconnection and connection state recovery.
Server package
Use the server package exclusively on your servers and in your serverless infrastructure. It provides two clients: a stateless Ably HTTP client, and the same realtime client that device packages use.
Ably HTTP client
Ably provides a REST API that exposes functionality such as message publishing, history, stats, and push operations. The Ably HTTP client is suitable for performing those operations when you don't need to subscribe to the updates. It communicates over HTTP, is effectively stateless and does not maintain a persistent connection to Ably. It is the most common choice for server-side operations.
Use an Ably HTTP client to:
- Publish messages.
- Retrieve message history, presence history, or application statistics.
- Perform request-response style operations.
- Issue authentication tokens on behalf of other clients.
Realtime client on a server
For the majority of server-side use cases, an Ably HTTP client is sufficient. You may require a realtime client on a server if:
- Your server needs to publish at a very high rate, as a realtime client is more efficient at publishing in large volumes.
- You require ultra-low latency, as publishing over a realtime client avoids repeated authentication, capability checks, and SSL handshakes.
- Your backend service has a reason to subscribe to messages or presence events directly in realtime.
REST HTTP API
Interacting with the REST HTTP API directly is fully supported. However, Ably recommends using 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.
Supported protocols
Whilst SDKs are the recommended method of integrating Ably in the majority of cases, there are alternatives available when your use case requires it.
They are less feature-rich than SDKs, however they require fewer resources in terms of memory and network overhead to run. They can also be used to build applications using frameworks that don't have an available Ably SDK.
The following other protocols are supported:
| Protocol | Description |
|---|---|
| MQTT | Translate between the Message Queuing Telemetry Transport (MQTT) and Ably's own protocol. Often used in remote devices with small footprints |
| SSE | Use Server Sent Events (SSE) to get a realtime stream of events from Ably, where using a full SDK is impractical. Often used when you have stringent memory restrictions and only need to subscribe to events, not publish them |
| Pusher Adapter | Quickly migrate from Pusher to Ably using the Pusher Adapter |
| PubNub Adapter | Quickly migrate from PubNub to Ably using the PubNub Adapter |