@atcute/firehose

中文文档
1.2.0发布于 2026/09/100BSDES Module近 30 天下载 1.4万

lightweight and cute XRPC subscription client for AT Protocol

原始使用文档
文档目录11 节
  1. usage
  2. subscribing to the relay firehose
  3. cancelling the subscription
  4. handling message types
  5. tracking cursor for resumption
  6. using multiple servers
  7. handling errors
  8. connection lifecycle callbacks
  9. updating options at runtime
  10. disabling message validation
  11. WebSocket options

@atcute/firehose

lightweight XRPC subscription client for AT Protocol.

npm install @atcute/firehose

this package provides a generic client for XRPC subscriptions - the WebSocket-based streaming protocol used by AT Protocol. it handles frame decoding, automatic reconnection, and optional schema validation.

for consuming the Bluesky network firehose specifically, consider using @atcute/jetstream instead, which provides a simpler JSON-based interface.

usage

subscribing to the relay firehose

import { FirehoseSubscription } from '@atcute/firehose';
import { ComAtprotoSyncSubscribeRepos } from '@atcute/atproto';

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
});

for await (const message of subscription) {
    console.log(message.$type, message.seq);
}

the connection opens when you start iterating and closes when you break out of the loop. the underlying WebSocket automatically reconnects on disconnection.

cancelling the subscription

pass an AbortSignal to close the connection and reject iteration:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    signal: AbortSignal.timeout(10_000),
});

for await (const message of subscription) {
    console.log(message.$type);
}

handling message types

messages include a $type field indicating their type:

for await (const message of subscription) {
    switch (message.$type) {
        case 'com.atproto.sync.subscribeRepos#commit': {
            // repository commit (record creates, updates, deletes)
            console.log('commit:', message.repo, message.rev);
            break;
        }

        case 'com.atproto.sync.subscribeRepos#handle': {
            // handle change
            console.log('handle:', message.did, message.handle);
            break;
        }

        case 'com.atproto.sync.subscribeRepos#migrate': {
            // account migration
            console.log('migrate:', message.did, message.migrateTo);
            break;
        }

        case 'com.atproto.sync.subscribeRepos#tombstone': {
            // account deletion
            console.log('tombstone:', message.did);
            break;
        }

        case 'com.atproto.sync.subscribeRepos#identity': {
            // identity update
            console.log('identity:', message.did);
            break;
        }

        case 'com.atproto.sync.subscribeRepos#account': {
            // account status change
            console.log('account:', message.did, message.active);
            break;
        }
    }
}

tracking cursor for resumption

use a function for params to provide the current cursor on each connection attempt:

let cursor: number | undefined;

// load saved cursor if resuming
const saved = localStorage.getItem('firehose-cursor');
if (saved) {
    cursor = Number(saved);
}

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    // function is called on each connection/reconnection
    params: () => ({ cursor }),
});

for await (const message of subscription) {
    if ('seq' in message) {
        cursor = message.seq;

        // periodically save cursor for recovery
        if (cursor % 1000 === 0) {
            localStorage.setItem('firehose-cursor', String(cursor));
        }
    }
}

the params function is called on each connection attempt, so when the WebSocket reconnects after a disconnection, it automatically uses the latest cursor value.

using multiple servers

pass an array of URLs for automatic failover. the client randomly selects one on each connection:

const subscription = new FirehoseSubscription({
    service: ['wss://bsky.network', 'wss://bsky-relay.example.com'],
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
});

handling errors

XRPC subscriptions can send error frames. handle them with the onError callback:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    onError(err) {
        console.error('firehose error:', err);
        // common errors:
        // - "FutureCursor": cursor is ahead of the server
        // - "ConsumerTooSlow": client is not consuming messages fast enough
    },
});

connection lifecycle callbacks

handle connection events for logging or UI updates:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    onConnectionOpen(event) {
        console.log('connected to firehose');
    },
    onConnectionClose(event) {
        console.log('disconnected:', event.code, event.reason);
    },
    onConnectionError(event) {
        console.error('connection error:', event.error);
    },
});

updating options at runtime

change options using updateOptions(). this triggers a reconnection:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
});

// later, switch to a different service
subscription.updateOptions({
    service: 'wss://different-relay.example.com',
});

disabling message validation

by default, messages are validated against the schema. disable this for better performance if you trust the server:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    validateEvents: false,
});

WebSocket options

pass options to the underlying partysocket WebSocket for custom reconnection behavior:

const subscription = new FirehoseSubscription({
    service: 'wss://bsky.network',
    nsid: ComAtprotoSyncSubscribeRepos.mainSchema,
    ws: {
        maxRetries: 10,
        minReconnectionDelay: 1000,
        maxReconnectionDelay: 30000,
    },
});

版本与发布记录

@atcute/firehose 共发布 7 个版本,首次发布于 2025/11/06;过去 12 个月发布了 6 个版本。

版本发布日期距今
1.2.0latest2026/09/101 个月前
1.1.02026/08/192 个月前
1.0.22026/07/163 个月前
1.0.12026/06/304 个月前
1.0.02026/05/095 个月前
0.1.12026/05/086 个月前

关于 @atcute/firehose 的常见问题

@atcute/firehose 是什么?

lightweight and cute XRPC subscription client for AT Protocol

如何安装 @atcute/firehose?

在项目目录运行 npm install @atcute/firehose,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@atcute/lexicons(^2.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atcute/firehose 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atcute__firehose 是否存在。

@atcute/firehose 支持 ESM 还是 CommonJS?

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

@atcute/firehose 可以商用吗?

@atcute/firehose 使用 0BSD 许可。几乎没有限制:可自由使用、修改和分发,连保留版权声明也不要求。以上为概括,不构成法律意见,请以许可原文为准。

@atcute/firehose 的下载量有多少?

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

@atcute/firehose 还在维护吗?

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