@windingtree/sdk-pubsub
v1.3.6
Published
libp2p PubSub protocol implementation for the WindingTree market protocol
Downloads
46
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
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:
CenterSub
: The main class that extendsGossipSub
and modifies it to suit a centralized PubSub model.MessagesCache
: A class used to manage and manipulate a cache of messages that are currently being processed by the network.MessageTransformer
: A user-defined function to parse and process incoming messages.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.
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
Testing
pnpm test