@authup/core-realtime-kit

中文文档
1.0.0-beta.73发布于 2026/11/02Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 2341

A socket.io client manager and event-name helpers for Authup's realtime entity events.

原始使用文档
文档目录9 节
  1. Where it fits
  2. Installation
  3. Usage
  4. Connect
  5. Listen for entity events
  6. Reconnect after a token change
  7. Options
  8. API
  9. License

Authup logo

@authup/core-realtime-kit

A socket.io client manager and event-name helpers for Authup's realtime entity events.
One manager per server: sockets per namespace, the access token sent on every handshake.

npm version CI License: Apache-2.0

Installation · Usage · API · Documentation


Where it fits

When an entity changes, Authup's server publishes the change through @authup/server-kit's socket handler, which emits it via a socket.io Redis emitter under a name such as userCreated. This package holds the two halves both sides agree on: buildEventFullName, which builds those names, and ClientManager, which a browser client uses to connect and listen. @authup/server-kit and @authup/client-web-kit depend on it. Authup itself runs no socket.io server: the events reach a socket.io server you operate on the same Redis. Install this package directly only when you build such a client yourself.

Installation

npm install @authup/core-realtime-kit socket.io-client

socket.io-client (^4.8.4) is a peer dependency: you install it. ESM only. Node.js >=26.0.0, or a browser bundler.

Usage

Connect

import { ClientManager } from '@authup/core-realtime-kit';

const manager = new ClientManager({
    url: 'https://realtime.example.com',
    token: () => localStorage.getItem('access_token') ?? undefined,
});

const socket = await manager.connect('/');
  • connect(namespace) resolves once the socket is connected, or rejects with the connect_error. The default namespace is /.
  • token is a string or a sync or async function. It is called on every handshake and sent as auth.token; a function that throws sends no token.
  • The underlying socket.io Manager is created with autoConnect: false; nothing connects until you call connect.
  • A path in url becomes the socket.io path: https://example.com/api connects with path: '/api/socket.io'. An options.path is appended to that prefix.

Listen for entity events

import { EventNameSuffix, buildEventFullName } from '@authup/core-realtime-kit';

socket.emit(buildEventFullName('user', EventNameSuffix.SUBSCRIBE));

socket.on(buildEventFullName('user', EventNameSuffix.CREATED), (payload) => {
    // payload.data is the created user, payload.meta.roomName the room it was sent to
});

buildEventFullName(entity, verb) concatenates the entity name with the capitalized verb: ('user', 'created') is userCreated.

Reconnect after a token change

await manager.reconnectAll();

The token is only read on the handshake, so a socket keeps the token it connected with. reconnect(namespace) and reconnectAll() disconnect and connect again to send the current one. disconnect(namespace) closes a socket and keeps it for later; eject(namespace) closes and forgets it.

Options

new ClientManager(ctx):

OptionTypeDefaultDescription
urlstringrequiredServer URL; its path becomes the socket.io path prefix
tokenstring | () => string | undefined | Promise<string | undefined>Sent as auth.token on every handshake
optionsPartial<ManagerOptions>Passed to the socket.io-client Manager

API

ExportKindPurpose
ClientManagerclassOne socket.io Manager, one socket per namespace: connect, disconnect, reconnect, reconnectAll, inject, eject, options
ClientManagerContext, ClientManagerTokenFntypeConstructor input and the token function
buildEventFullNamefunctionentity + capitalized verb
EventNameSuffixenumcreated, updated, deleted, subscribe, unsubscribe
EventFullName, EventPayloadtypeThe event name template and the payload shape
isEventTarget, isEventCallbackfunctionGuards for socket handler arguments
STCEvents, CTSEvents, STSEvents, STCEventContext, EventCallback, DefaultEventstypeEvent map types for typed sockets

License

Made with 💚

Published under the Apache 2.0 License.

版本与发布记录

@authup/core-realtime-kit 共发布 59 个版本,首次发布于 2024/05/08;过去 12 个月发布了 44 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.0-beta.73latest2026/11/026 天前
1.0.0-beta.722026/10/2711 天前
1.0.0-beta.712026/10/2018 天前
1.0.0-beta.702026/10/1919 天前
1.0.0-beta.692026/10/1424 天前
1.0.0-beta.682026/09/251 个月前

关于 @authup/core-realtime-kit 的常见问题

@authup/core-realtime-kit 是什么?

A socket.io client manager and event-name helpers for Authup's realtime entity events.

如何安装 @authup/core-realtime-kit?

在项目目录运行 npm install @authup/core-realtime-kit,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:socket.io-client(^4.8.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@authup/core-realtime-kit 支持 TypeScript 吗?

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

@authup/core-realtime-kit 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@authup/core-realtime-kit 需要什么版本的 Node.js?

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

@authup/core-realtime-kit 可以商用吗?

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

@authup/core-realtime-kit 的下载量有多少?

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

@authup/core-realtime-kit 还在维护吗?

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