
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
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.
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);
});
}
});
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 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 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.
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:
Everything you need to get started with Ably:
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:
Platform | Support |
---|---|
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.
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 });
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.
// Initialize Ably Realtime client
const realtimeClient = new Ably.Realtime({ key: 'your-ably-api-key', clientId: 'me' });
// Wait for connection to be established
await realtimeClient.connection.once('connected');
console.log('Connected to Ably');
// Get a reference to the 'test-channel' channel
const channel = realtimeClient.channels.get('test-channel');
// Subscribe to all messages published to this channel
await channel.subscribe((message) => {
console.log(`Received message: ${message.data}`);
});
// Publish a test message to the channel
await channel.publish('test-event', 'hello world');
The Pub/Sub SDK has a modular (tree-shakable) variant to build with a small bundle sizes.
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:
BaseRealtime
class;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', // Replace with a real key from the Ably dashboard
plugins: {
WebSocketTransport,
FetchRequest,
RealtimePresence,
},
});
You must provide:
FetchRequest
or XHRRequest
;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:
logLevel
of 1 (that is, errors)If you require more verbose logging, use the default variant of the SDK.
For more information view the TypeDoc references.
Read the CONTRIBUTING.md guidelines to contribute to Ably.
The CHANGELOG.md contains details of the latest releases for this SDK. You can also view all Ably releases on changelog.ably.com.
For help or technical support, visit Ably's support page or GitHub Issues for community-reported bugs and discussions.
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.
If you are using this SDK in a service worker, note:
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:
To ensure compatibility, add the following to your manifest.json
:
{
// ...
"minimum_chrome_version": "116",
// ...
}
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.
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.
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
.
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.
If you encounter an error such as connection limit exceeded
during development, it may be caused by several issues.
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' });
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.
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.
FAQs
Realtime client library for Ably, the realtime messaging service
The npm package ably receives a total of 107,830 weekly downloads. As such, ably popularity was classified as popular.
We found that ably demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 8 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.