# @windingtree/sdk-pubsub

> libp2p PubSub protocol implementation for the WindingTree market protocol

Latest version **1.3.6** (published 2024-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @windingtree/sdk-pubsub
pnpm add @windingtree/sdk-pubsub
yarn add @windingtree/sdk-pubsub
bun add @windingtree/sdk-pubsub
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.6 |
| Published | 2024-03-11 |
| First published | 2023-07-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 12 |
| Unpacked size | 76.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Kostiantyn Smyrnov |
| Maintainers | kostysh, kvakes, tomashq |

## Links

- npm: https://www.npmjs.com/package/@windingtree/sdk-pubsub
- Repository: https://github.com/windingtree/sdk.git#readme
- Homepage: https://github.com/windingtree/sdk/tree/readme#readme
- Issues: https://github.com/windingtree/sdk/issues
- npm.io page: https://npm.io/package/@windingtree/sdk-pubsub

## Dependencies (12)

- [libp2p](https://npm.io/package/libp2p.md) ^0.46.14
- [superjson](https://npm.io/package/superjson.md) ^2.2.1
- [multiformats](https://npm.io/package/multiformats.md) ^12.1.3
- [@libp2p/peer-id](https://npm.io/package/@libp2p/peer-id.md) ^4.0.6
- [@libp2p/interface](https://npm.io/package/@libp2p/interface.md) ^0.1.3
- [@windingtree/sdk-types](https://npm.io/package/@windingtree/sdk-types.md) 1.1.4
- [@windingtree/sdk-utils](https://npm.io/package/@windingtree/sdk-utils.md) 1.2.3
- [@multiformats/multiaddr](https://npm.io/package/@multiformats/multiaddr.md) ^12.1.3
- [@windingtree/sdk-logger](https://npm.io/package/@windingtree/sdk-logger.md) 1.1.4
- [@windingtree/sdk-storage](https://npm.io/package/@windingtree/sdk-storage.md) 1.5.0
- [@windingtree/sdk-constants](https://npm.io/package/@windingtree/sdk-constants.md) 1.2.3
- [@chainsafe/libp2p-gossipsub](https://npm.io/package/@chainsafe/libp2p-gossipsub.md) ^10.1.0

## Recent versions

- 1.3.6 (latest) — 2024-03-11
- 1.3.3-beta.0 (beta) — 2024-01-17
- 1.3.5 — 2024-02-15
- 1.3.4 — 2024-01-26
- 1.3.3 — 2024-01-17
- 1.3.2 — 2023-12-22
- 1.3.1 — 2023-12-11
- 1.3.0 — 2023-11-29
- 1.2.2 — 2023-10-31
- 1.2.1 — 2023-10-31
- 1.2.0 — 2023-10-25
- 1.2.0-beta.0 — 2023-10-25
- 1.1.1-beta.0 — 2023-07-31
- 1.1.0 — 2023-07-27
- 1.1.0-beta.3 — 2023-07-27
- … 3 more at https://npm.io/package/@windingtree/sdk-pubsub/versions

## README

# @windingtree/sdk-pubsub

This package provides a centralized publish/subscribe protocol (`CenterSub`) for peer-to-peer communication within a WindingTree network, built on top of the `libp2p-gossipsub` protocol.

## Installation

```bash
pnpm i @windingtree/sdk-pubsub
```

## Key concepts

This library introduces a custom PubSub protocol (`CenterSub`) for the WindingTree network. It is based on the GossipSub protocol from the ChainSafe library but modifies certain aspects to adapt to a centralized system.

The key concepts in this library include:

1. `CenterSub`: The main class that extends `GossipSub` and modifies it to suit a centralized PubSub model.

2. `MessagesCache`: A class used to manage and manipulate a cache of messages that are currently being processed by the network.

3. `MessageTransformer`: A user-defined function to parse and process incoming messages.

4. `handlePeerConnect`, `onAddPeer`, `onRemovePeer`, `onHandleReceivedMessage`, `onSelectPeersToPublish`: Overridden GossipSub methods to adapt the protocol to the centralized model.

## Usage

This package is primarily used to manage the PubSub protocol for a WindingTree network. An instance of the `CenterSub` class can be created and used for message broadcasting and handling.

```typescript
import { createLibp2p } from 'libp2p';
import { CenterSubOptions, centerSub } from '@windingtree/sdk-pubsub';

const options: CenterSubOptions = {
  isClient: true,
  directPeers: [...],
  messageTransformer: (message: ArrayBuffer) => {
    // parse and process the message
    return JSON.parse(decodeText(message)) as GenericMessage;
  },
  messagesStorage: new Storage(),
};

const createCenterSubInstance = centerSub(options);

this.libp2p = await createLibp2p({
  transports: [...],
  streamMuxers: [...],
  connectionEncryption: [...],
  services: {
    pubsub: centerSub({ // <-- Using CenterSub as pubsub service
      isClient: true,
      directPeers: [
        {
          id: /* serverPeerId */,
          addrs: [/* serverMultiaddr */],
        },
      ],
    }),
  },
  connectionManager: {...},
);
```

## Documentation

For full documentation and examples, visit [windingtree.github.io/sdk](https://windingtree.github.io/sdk)

## Testing

```bash
pnpm test
```

## Contributing

[Contribution guidelines](https://windingtree.github.io/sdk/#/docs/contribution)

---
_Source: https://npm.io/package/@windingtree/sdk-pubsub · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
