@apinator/client

中文文档
2.3.0发布于 2026/10/25MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2531

JavaScript client SDK for Apinator — real-time WebSocket messaging

原始使用文档
文档目录16 节
  1. Features
  2. Installation
  3. Without a bundler
  4. Node.js
  5. Quick Start
  6. Channel Types
  7. Public Channels
  8. Private Channels
  9. Presence Channels
  10. Cache Channels
  11. Client Events
  12. Connection States
  13. API Reference
  14. Browser Support
  15. Links
  16. License

@apinator/client

npm version License: MIT CI

JavaScript client SDK for Apinator — real-time WebSocket messaging for web applications.

Features

  • Public, private, presence, and cache channels
  • Automatic reconnection with exponential backoff
  • Client events on private/presence channels
  • Presence member tracking
  • TypeScript-first with full type definitions
  • Zero dependencies — works in any modern browser
  • ESM, CommonJS and script-tag builds

Installation

npm install @apinator/client

Without a bundler

Save this as index.html and open it. The script tag defines a global Apinator:

<!DOCTYPE html>
<html>
  <body>
    <script src="https://cdn.jsdelivr.net/npm/@apinator/[email protected]/dist/apinator.min.js"></script>
    <script>
      const client = new Apinator({
        appKey: 'your-app-key',
        cluster: 'eu',
      });

      client.connect();

      const channel = client.subscribe('my-channel');

      channel.bind('my-event', (data) => {
        console.log('Received:', data);
      });
    </script>
  </body>
</html>

Then publish my-event on my-channel from your server (for example with @apinator/server) and it is logged in the browser console.

Prefer ES modules? Import from the module build instead:

<script type="module">
  import { Apinator } from 'https://cdn.jsdelivr.net/npm/@apinator/[email protected]/dist/index.mjs';
</script>

Node.js

Node 22+ has a global WebSocket. On older versions, install ws and pass it in:

import WebSocket from 'ws';
import { Apinator } from '@apinator/client';

const client = new Apinator({ appKey: 'your-app-key', cluster: 'eu', WebSocket });

Without one, connect() logs No WebSocket implementation found…, reports it to bind('realtime:error', ...) and moves to unavailable instead of retrying.

Quick Start

import { Apinator } from '@apinator/client';

const client = new Apinator({
  appKey: 'your-app-key',
  cluster: 'eu',
});

client.connect();

const channel = client.subscribe('my-channel');

channel.bind('my-event', (data) => {
  console.log('Received:', data);
});

Channel Types

Public Channels

No authentication required. Any client can subscribe.

const channel = client.subscribe('news');
channel.bind('update', (data) => { /* ... */ });

Private Channels

Require server-side authentication. Prefix with private-.

const client = new Apinator({
  appKey: 'your-app-key',
  cluster: 'eu',
  authEndpoint: '/api/realtime/auth',
});

const channel = client.subscribe('private-orders');
channel.bind('new-order', (data) => { /* ... */ });

Presence Channels

Like private channels, but also track who is subscribed. Prefix with presence-.

const presence = client.subscribe('presence-chat') as PresenceChannel;

presence.bind('realtime:subscription_succeeded', () => {
  console.log('Members:', presence.getMembers());
  console.log('Me:', presence.me);
});

presence.bind('realtime:member_added', (member) => {
  console.log('Joined:', member);
});

presence.bind('realtime:member_removed', (member) => {
  console.log('Left:', member);
});

channel.me is derived from the signed channel_data returned by your authEndpoint. It becomes available only after realtime:subscription_succeeded.

Cache Channels

A channel named cache-, private-cache- or presence-cache- keeps its last event for 30 minutes. A client that subscribes late gets it right after realtime:subscription_succeeded, so bind your handlers straight after subscribe().

const job = client.subscribe(`private-cache-job-${jobId}`);
job.bind('job-finished', (data) => { /* runs even if the job finished before you subscribed */ });

If nothing is cached (or it has expired) no event arrives, so keep a fallback such as a status API call.

Client Events

Trigger events directly from the client on private or presence channels:

const privateChannel = client.subscribe('private-chat');
privateChannel.trigger('client-typing', { user: 'alice' });

Connection States

Monitor the connection lifecycle:

client.bind('state_change', ({ previous, current }) => {
  console.log(`${previous} -> ${current}`);
});

States: initialized → connecting → connected → disconnected / unavailable

API Reference

See docs/api-reference.md for the full API.

Browser Support

Any browser with WebSocket and fetch support:

  • Chrome 42+
  • Firefox 39+
  • Safari 10.1+
  • Edge 14+
  • Brave 10+

License

MIT — see LICENSE.

版本与发布记录

@apinator/client 共发布 11 个版本,首次发布于 2026/02/17;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.3.0latest2026/10/2513 天前
2.2.22026/10/1721 天前
2.2.12026/10/1523 天前
2.2.02026/10/1424 天前
2.1.02026/10/1424 天前
2.0.02026/02/178 个月前

关于 @apinator/client 的常见问题

@apinator/client 是什么?

JavaScript client SDK for Apinator — real-time WebSocket messaging

如何安装 @apinator/client?

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

@apinator/client 支持 TypeScript 吗?

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

@apinator/client 支持 ESM 还是 CommonJS?

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

@apinator/client 可以商用吗?

@apinator/client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@apinator/client 的下载量有多少?

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

@apinator/client 还在维护吗?

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