@chainsafe/libp2p-gossipsub

中文文档
14.1.2发布于 2025/09/18Apache-2.0TypeScriptES Module近 30 天下载 8.4万

A typescript implementation of gossipsub

原始使用文档
文档目录8 节
  1. Table of Contents
  2. Specs
  3. Install
  4. Usage
  5. API
  6. Create a gossipsub implementation
  7. Contribute
  8. License

js-libp2p-gossipsub

Travis CI ES Version Node Version

Table of Contents

Specs

Gossipsub is an implementation of pubsub based on meshsub and floodsub. You can read the specification here.

libp2p-gossipsub currently implements the v1.1 of the spec.

Install

npm install @chainsafe/libp2p-gossipsub

Usage

import { gossipsub } from '@chainsafe/libp2p-gossipsub'


const libp2p = await createLibp2p({
  // ...
  services: {
    pubsub: gossipsub(options)
  }
});

libp2p.services.pubsub.addEventListener('message', (message) => {
  console.log(`${message.detail.topic}:`, new TextDecoder().decode(message.detail.data))
})

libp2p.services.pubsub.subscribe('fruit')

libp2p.services.pubsub.publish('fruit', new TextEncoder().encode('banana'))

API

Create a gossipsub implementation

const options = {…}
const gossipsub = gossipsub(options)(libp2p)

Options is an optional object with the following key-value pairs:

  • emitSelf: boolean identifying whether the node should emit to self on publish, in the event of the topic being subscribed (defaults to false).
  • gossipIncoming: boolean identifying if incoming messages on a subscribed topic should be automatically gossiped (defaults to true).
  • fallbackToFloodsub: boolean identifying whether the node should fallback to the floodsub protocol, if another connecting peer does not support gossipsub (defaults to true).
  • floodPublish: boolean identifying if self-published messages should be sent to all peers, (defaults to true).
  • doPX: boolean identifying whether PX is enabled; this should be enabled in bootstrappers and other well connected/trusted nodes (defaults to false).
  • msgIdFn: a function with signature (message) => string defining the message id given a message, used internally to deduplicate gossip (defaults to (message) => message.from + message.seqno.toString('hex'))
  • signMessages: boolean identifying if we want to sign outgoing messages or not (default: true)
  • strictSigning: boolean identifying if message signing is required for incoming messages or not (default: true)
  • messageCache: optional, a customized MessageCache instance, see the implementation for the interface.
  • scoreParams: optional, a customized peer score parameters Object.
  • scoreThresholds: optional, a customized peer score thresholds Object.
  • directPeers: optional, an array of AddrInfo of peers with which we will maintain direct connections.

For the remaining API, see @libp2p/interface-pubsub.

Contribute

This module is actively under development. Please check out the issues and submit PRs!

License

MIT © ChainSafe Systems

版本与发布记录

@chainsafe/libp2p-gossipsub 共发布 53 个版本,首次发布于 2022/05/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
14.1.2latest2025/09/181.1 年前
14.1.12025/03/281.6 年前
14.1.02024/09/132 年前
14.0.02024/09/112 年前
13.2.02024/08/202 年前
13.1.12024/08/122 年前

关于 @chainsafe/libp2p-gossipsub 的常见问题

@chainsafe/libp2p-gossipsub 是什么?

A typescript implementation of gossipsub

如何安装 @chainsafe/libp2p-gossipsub?

在项目目录运行 npm install @chainsafe/libp2p-gossipsub,或使用 pnpm add / yarn add / bun add。

@chainsafe/libp2p-gossipsub 支持 TypeScript 吗?

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

@chainsafe/libp2p-gossipsub 支持 ESM 还是 CommonJS?

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

@chainsafe/libp2p-gossipsub 可以商用吗?

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

@chainsafe/libp2p-gossipsub 的下载量有多少?

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

@chainsafe/libp2p-gossipsub 还在维护吗?

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