@ably/chat

中文文档
2.0.0发布于 2026/11/02Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 146.1万

Ably Chat is a set of purpose-built APIs for a host of chat features enabling you to create 1:1, 1:Many, Many:1 and Many:Many chat rooms for any scale. It is designed to meet a wide range of chat use cases, such as livestreams, in-game communication, cust

原始使用文档
文档目录10 节
  1. Getting started
  2. Supported platforms
  3. Installation
  4. JavaScript/React
  5. Usage
  6. JavaScript / TypeScript
  7. React
  8. Releases
  9. Contribute
  10. Support, feedback, and troubleshooting

Ably Chat Header npm version License

Ably Chat JavaScript, TypeScript and React SDK

Ably Chat is a set of purpose-built APIs for a host of chat features enabling you to create 1:1, 1:Many, Many:1 and Many:Many chat rooms for any scale. It is designed to meet a wide range of chat use cases, such as livestreams, in-game communication, customer support, or social interactions in SaaS products. Built on Ably's core service, it abstracts complex details to enable efficient chat architectures.


Getting started

Everything you need to get started with Ably:


Supported platforms

Ably aims to support a wide range of platforms. If you experience any compatibility issues, open an issue in the repository or contact Ably support.

This SDK supports the following platforms:

PlatformSupport
BrowsersAll major desktop and mobile browsers, including Chrome, Firefox, Edge, Safari (iOS/macOS), Opera, and Android. Internet Explorer is not supported.
Node.jsVersion 22 or newer.
TypeScriptFully supported, the library is written in TypeScript.
ReactIncludes providers and hooks for deep integration with the React ecosystem.
React NativeAll React Native platforms. Issues can be reported or support requested.
AndroidSupported via the Ably Chat Kotlin SDK.
iOSSupported via the Ably Chat Swift SDK.

Installation

The Chat SDK is built on top of the Ably Pub/Sub SDK and uses that to establish a connection with Ably.

JavaScript/React

Install the Pub/Sub SDK and the Chat SDK:

npm install @ably/pubsub-device @ably/chat

Usage

JavaScript / TypeScript

The following code connects to Ably's chat service, subscribes to a chat room, and sends a message to that room:

import { createClient } from '@ably/pubsub-device';
import { createChatClient, RoomStatus, RoomStatusChange } from '@ably/chat';

// Initialize Ably Pub/Sub Realtime client
// Note: For client-side applications, token authentication is recommended.
// See: https://ably.com/docs/auth
const pubsubClient = createClient({
  key: '<your-ably-api-key>',
  clientId: 'your-client-id',
});

// Create a chat client
const chatClient = createChatClient(pubsubClient);

// Get a chat room
const room = await chatClient.rooms.get('my-room');

// Monitor room status
room.onStatusChange((statusChange: RoomStatusChange) => {
  switch (statusChange.current) {
    case RoomStatus.Attached:
      console.log('Room is attached');
      break;
    case RoomStatus.Detached:
      console.log('Room is detached');
      break;
    case RoomStatus.Failed:
      console.log('Room failed:', statusChange.error);
      break;
    default:
      console.log('Room status:', statusChange.current);
  }
});

// Subscribe to messages
const subscription = room.messages.subscribe((event) => {
  console.log('Received message:', event.message.text);
});

// Attach to the room
await room.attach();

// Send a message
await room.messages.send({ text: 'Hello, World!' });

React

For React applications, the SDK provides hooks and providers for seamless integration:

import { createClient } from '@ably/pubsub-device';
import { createChatClient } from '@ably/chat';
import { ChatClientProvider, ChatRoomProvider, useMessages } from '@ably/chat/react';

// Initialize clients
// Note: For client-side applications, token authentication is recommended.
// See: https://ably.com/docs/auth
const pubsubClient = createClient({
  key: '<your-ably-api-key>',
  clientId: 'your-client-id',
});
const chatClient = createChatClient(pubsubClient);

// Wrap your app with providers
function App() {
  return (
    <ChatClientProvider client={chatClient}>
      <ChatRoomProvider name="my-room">
        <ChatComponent />
      </ChatRoomProvider>
    </ChatClientProvider>
  );
}

// Use hooks to interact with chat
function ChatComponent() {
  const { sendMessage } = useMessages({
    listener: (event) => {
      console.log('Received message:', event.message.text);
    },
  });

  const handleSend = async () => {
    await sendMessage({ text: 'Hello, World!' });
  };

  return <button onClick={handleSend}>Send Message</button>;
}

Releases

The CHANGELOG.md contains details of the latest releases for this SDK. You can also view all Ably releases on changelog.ably.com.


Contribute

Read the CONTRIBUTING.md guidelines to contribute to Ably or Share feedback or request a new feature.


Support, feedback, and troubleshooting

For help or technical support, visit Ably's support page. You can also view the community reported Github issues or raise one yourself.

版本与发布记录

@ably/chat 共发布 28 个版本,首次发布于 2024/07/10;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/11/023 天前
0.0.0-canary.20261001T1209.783b80e6canary2026/10/011 个月前
1.4.02026/06/015 个月前
1.3.12026/03/267 个月前
1.3.02026/03/048 个月前
1.2.02026/02/258 个月前

关于 @ably/chat 的常见问题

@ably/chat 是什么?

Ably Chat is a set of purpose-built APIs for a host of chat features enabling you to create 1:1, 1:Many, Many:1 and Many:Many chat rooms for any scale. It is designed to meet a wide range of chat use cases, such as livestreams, in-game communication, cust

如何安装 @ably/chat?

在项目目录运行 npm install @ably/chat,或使用 pnpm add / yarn add / bun add。

@ably/chat 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ably/chat 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ably/chat 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ably/chat 可以商用吗?

@ably/chat 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ably/chat 的下载量有多少?

根据 npm 官方下载统计,@ably/chat 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,461,175 次,日均约 48,706 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ably/chat 还在维护吗?

最新版本 2.0.0 发布于 2026/11/02(3 天前);至今共发布 28 个版本,其中过去 12 个月发布了 7 个;首次发布于 2024/07/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。