What is ably?
Ably is a real-time messaging service that allows you to build powerful real-time applications. It provides features such as pub/sub messaging, presence, message history, and more, making it suitable for chat applications, live updates, and collaborative tools.
What are ably's main functionalities?
Pub/Sub Messaging
This feature allows you to publish and subscribe to messages on a channel. The code sample demonstrates how to publish a message to a channel and subscribe to receive messages from that channel.
const Ably = require('ably');
const realtime = new Ably.Realtime('YOUR_API_KEY');
const channel = realtime.channels.get('test-channel');
// Publishing a message
channel.publish('greeting', 'Hello, world!', (err) => {
if (err) {
console.error('Unable to publish message; err = ' + err.message);
} else {
console.log('Message successfully sent');
}
});
// Subscribing to a channel
channel.subscribe('greeting', (message) => {
console.log('Received message: ' + message.data);
});
Presence
This feature allows you to track the presence of users in a channel. The code sample demonstrates how to enter presence and subscribe to presence events to track when users join the channel.
const Ably = require('ably');
const realtime = new Ably.Realtime('YOUR_API_KEY');
const channel = realtime.channels.get('presence-channel');
// Entering presence
channel.presence.enter('user1', (err) => {
if (err) {
console.error('Unable to enter presence; err = ' + err.message);
} else {
console.log('Entered presence');
}
});
// Subscribing to presence events
channel.presence.subscribe('enter', (member) => {
console.log('Member entered: ' + member.clientId);
});
Message History
This feature allows you to retrieve the history of messages sent on a channel. The code sample demonstrates how to fetch and display past messages from a channel.
const Ably = require('ably');
const realtime = new Ably.Realtime('YOUR_API_KEY');
const channel = realtime.channels.get('history-channel');
// Retrieving message history
channel.history((err, resultPage) => {
if (err) {
console.error('Unable to retrieve message history; err = ' + err.message);
} else {
resultPage.items.forEach((message) => {
console.log('Message: ' + message.data);
});
}
});
Other packages similar to ably
pusher
Pusher is a service that provides real-time messaging and event broadcasting. It offers similar pub/sub messaging, presence, and message history features. Pusher is known for its ease of use and extensive documentation, making it a strong competitor to Ably.
socket.io
Socket.IO is a library that enables real-time, bidirectional, and event-based communication. It is widely used for building real-time applications and supports features like pub/sub messaging and presence. Unlike Ably, Socket.IO requires you to manage your own server infrastructure.
pubnub
PubNub is a real-time messaging service that offers pub/sub messaging, presence, and message history features. It is known for its global infrastructure and low-latency messaging, making it suitable for applications requiring high performance and reliability.

Ably Pub/Sub JavaScript SDK
Build any realtime experience using Ably’s Pub/Sub JavaScript SDK. Supported on all popular platforms and frameworks, including Node, React, and Web Workers.
Ably Pub/Sub provides flexible APIs that deliver features such as pub-sub messaging, message history, presence, and push notifications. Utilizing Ably’s realtime messaging platform, applications benefit from its highly performant, reliable, and scalable infrastructure.
Ably LiveObjects is also available as a Pub/Sub JavaScript SDK plugin. You can use LiveObjects to synchronize application state across your clients at scale.
Find out more:
Getting started
Everything you need to get started with Ably:
Supported platforms
Ably aims to support a wide range of platforms and all current browser versions, including Chrome, Firefox, Safari, Microsoft Edge, and other modern browsers. If you experience any compatibility issues, open an issue in the repository or contact Ably support.
The following platforms are supported:
JavaScript | ES2017 |
Node.js | See engines in package.json. |
React | >=16.8.x |
TypeScript | Type definitions are included in the package. |
Web Workers | Browser bundle and modular support. |
[!NOTE]
Versions 1.2.x of the SDK support Internet Explorer >=9 and other older browsers, as well as Node.js >=8.17.
[!IMPORTANT]
SDK versions < 1.2.36 will be deprecated from November 1, 2025.
Installation
The JavaScript SDK is available as an NPM module. To get started with your project, install the package:
npm install ably
Run the following to instantiate a client:
import * as Ably from 'ably';
const realtime = new Ably.Realtime({ key: apiKey });
Usage
The following code connects to Ably's realtime messaging service, subscribes to a channel to receive messages, and publishes a test message to that same channel.
const realtimeClient = new Ably.Realtime({ key: 'your-ably-api-key', clientId: 'me' });
await realtimeClient.connection.once('connected');
console.log('Connected to Ably');
const channel = realtimeClient.channels.get('test-channel');
await channel.subscribe((message) => {
console.log(`Received message: ${message.data}`);
});
await channel.publish('test-event', 'hello world');
Modular variant
The Pub/Sub SDK has a modular (tree-shakable) variant to build with a small bundle sizes.
Modular variant details.
Aimed at those who are concerned about their app's bundle size, the modular variant of the library allows you to create a client which has only the functionality that you choose. Unused functionality can then be tree-shaken by your module bundler.
The modular variant of the library provides:
- A
BaseRealtime
class;
- various plugins that add functionality to a
BaseRealtime
instance, such as Rest
, RealtimePresence
, etc.
To use this variant of the library, import the BaseRealtime
class from ably/modular
, along with the plugins that you wish to use. Then, pass these plugins to the BaseRealtime
constructor as shown in the example below:
import { BaseRealtime, WebSocketTransport, FetchRequest, RealtimePresence } from 'ably/modular';
const client = new BaseRealtime({
key: 'YOUR_ABLY_API_KEY',
plugins: {
WebSocketTransport,
FetchRequest,
RealtimePresence,
},
});
You must provide:
- At least one HTTP request implementation; that is, one of
FetchRequest
or XHRRequest
;
- At least one realtime transport implementation; that is, one of
WebSocketTransport
or XHRPolling
.
BaseRealtime
offers the same API as the Realtime
class described in the rest of this README
. This means that you can develop an application using the default variant of the SDK and switch to the modular version when you wish to optimize your bundle size.
In order to further reduce bundle size, the modular variant of the SDK performs less logging than the default variant. It only logs:
- Messages that have a
logLevel
of 1 (that is, errors)
- A small number of other network events
If you require more verbose logging, use the default variant of the SDK.
For more information view the TypeDoc references.
Contribute
Read the CONTRIBUTING.md guidelines to contribute to Ably.
Releases
The CHANGELOG.md contains details of the latest releases for this SDK. You can also view all Ably releases on changelog.ably.com.
Support, feedback, and troubleshooting
For help or technical support, visit Ably's support page or GitHub Issues for community-reported bugs and discussions.
Chrome extensions
Ably Pub/Sub works out-of-the-box in background scripts for Chrome extensions using manifest v2. However, since manifest v3 background pages are no longer supported, you will need to run Ably Pub/Sub JavaScript SDK inside a service worker.
Chrome extensions support details.
If you are using this SDK in a service worker, note:
- In versions of Chrome before 116, active WebSockets would not reset the 30s service worker idle timer, resulting in the client being closed prematurely.
- In versions 116 and above, service workers will stay active as long as a client is connected.
To ensure compatibility, add the following to your manifest.json
:
If you are using this SDK's realtime features, for example, WebSockets in a service worker, note:
- In versions of Chrome before 116, active WebSockets would not reset the 30s service worker idle timer, resulting in the client being closed prematurely.
- In versions 116 and above, service workers will stay active as long as a client is connected.
To ensure compatibility, add the following to your manifest.json
:
{
"minimum_chrome_version": "116",
}
"Connection limit exceeded" errors during development
If you're hitting a "connection limit exceeded" error and see rising connection counts in your Ably dashboard, it's likely due to multiple Ably.Realtime
instances being created during development.
"Connection limit exceeded" support details.
Even for use client
components, Next.js may execute them on the server during pre-rendering. This can create unintended Ably.Realtime
connections from Node.js that remain open until you restart the development server.
Prevent server-side connections using autoConnect
and a window check:
const client = new Ably.Realtime({
key: 'your-ably-api-key',
autoConnect: typeof window !== 'undefined',
});
Creating the client inside React components can lead to a new connection on every render. To prevent this, move the new Ably.Realtime()
call outside of component functions.
In development environments that use Hot Module Replacement (HMR), such as React, Vite, or Next.js, saving a file can recreate the Ably.Realtime client, while previous instances remain connected. Over time, this leads to a growing number of active connections with each code edit. To fix: Move the client to a separate file (e.g., ably-client.js
) and import it. This ensures the client is recreated only when that file changes.
Next.js with App Router and Turbopack
If you encounter a Failed to compile Module not found
error or warnings related to keyv
when using Ably Pub/Sub JavaScript SDK with Next.js, add ably
to the serverComponentsExternalPackages
list in next.config.js
.
Next.js with App Router and Turbopack support details.
The following example adds ably
to the serverComponentsExternalPackages
list in next.config.js
:
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['ably'],
},
};
The issue is coming from the fact that when using App Router specifically, dependencies used inside Server Components and Route Handlers will automatically be bundled by Next.js. This causes issues with some packages, usually the ones that have complex require
statements, for example, requiring some packages dynamically during runtime. keyv
is one of those packages as it uses require
statement dynamically when requiring its adapters (see code in repo):
keyv
ends up being one of ably-js
's upstream dependencies for the node.js bundle, which causes the errors above when using it with Next.js App Router.
Using serverComponentsExternalPackages
opts out from using Next.js bundling for specific packages and uses native Node.js require
instead.
This is a common problem in App Router for a number of packages (for example, see next.js issue vercel/next.js#52876), and using serverComponentsExternalPackages
is the recommended approach here.
Genral errors during development
If you encounter an error such as connection limit exceeded
during development, it may be caused by several issues.
General errors support details.
Server-side rendering (SSR)
Use the autoConnect
option to prevent the client from connecting when rendered on the server:
const client = new Ably.Realtime({ key: 'your-ably-api-key', autoConnect: typeof window !== 'undefined' });
Component re-renders
Avoid creating the client inside React components. Instead, move the client instantiation outside of the component to prevent it from being recreated on every render.
Hot module replacement (HMR)
To avoid duplicate client instances caused by hot reloads, move the new Ably.Realtime()
call into a separate file, for example, ably.js
and export the client from there. This ensures a single shared instance is reused during development.