Class PubsubBaseProtocol

PubsubBaseProtocol handles the peers and connections logic for pubsub routers and specifies the API that pubsub routers should have.

Hierarchy

Index

References

BufferList

Re-exports BufferList

Connection

Re-exports Connection

Events

Re-exports Events

InMessage

Re-exports InMessage

Libp2p

Re-exports Libp2p

MuxedStream

Re-exports MuxedStream

PeerId

Re-exports PeerId

RPC

Re-exports RPC

RPCMessage

Re-exports RPCMessage

RPCSubOpts

Re-exports RPCSubOpts

SignaturePolicy

Re-exports SignaturePolicy

SignaturePolicyType

Re-exports SignaturePolicyType

message

Re-exports message

utils

Re-exports utils

Constructors

constructor

  • new PubsubBaseProtocol(__namedParameters: { canRelayMessage: undefined | false | true; debugName: string; emitSelf: undefined | false | true; globalSignaturePolicy: undefined | "StrictSign" | "StrictNoSign"; libp2p: any; multicodecs: string | string[] }): PubsubBaseProtocol
  • abstract

    Parameters

    • __namedParameters: { canRelayMessage: undefined | false | true; debugName: string; emitSelf: undefined | false | true; globalSignaturePolicy: undefined | "StrictSign" | "StrictNoSign"; libp2p: any; multicodecs: string | string[] }
      • canRelayMessage: undefined | false | true
      • debugName: string
      • emitSelf: undefined | false | true
      • globalSignaturePolicy: undefined | "StrictSign" | "StrictNoSign"
      • libp2p: any
      • multicodecs: string | string[]

    Returns PubsubBaseProtocol

Properties

_libp2p

_libp2p: any

_registrarId

_registrarId: any

canRelayMessage

canRelayMessage: boolean

If router can relay received messages, even if not subscribed

emitSelf

emitSelf: boolean

if publish should emit to self, if subscribed

globalSignaturePolicy

globalSignaturePolicy: string

The signature policy to follow by default

log

log: any

multicodecs

multicodecs: Array<string>

peerId

peerId: PeerId

peers

peers: Map<string, PeerStreams>

Map of peer streams

registrar

registrar: any

started

started: boolean

subscriptions

subscriptions: Set<string>

List of our subscriptions

topicValidators

topicValidators: Map<string, (arg0: string, arg1: InMessage) => Promise<void>>

Topic validator map

Keyed by topic Topic validators are functions with the following input:

topics

topics: Map<string, Set<string>>

Map of topics to which peers are subscribed to

Methods

_acceptFrom

  • _acceptFrom(id: string): boolean
  • Whether to accept a message from a peer Override to create a graylist

    override

    Parameters

    • id: string

    Returns boolean

Protected _addPeer

  • Notifies the router that a peer has been connected

    Parameters

    • peerId: PeerId
    • protocol: string

    Returns PeerStreams

Protected _buildMessage

  • Normalizes the message and signs it, if signing is enabled. Should be used by the routers to create the message to send.

    Parameters

    Returns Promise<RPCMessage>

_decodeRpc

  • _decodeRpc(bytes: Uint8Array): RPC
  • Decode Uint8Array into an RPC object. This can be override to use a custom router protobuf.

    Parameters

    • bytes: Uint8Array

    Returns RPC

_emitMessage

  • Emit a message from a peer

    Parameters

    Returns void

_encodeRpc

  • _encodeRpc(rpc: RPC): Uint8Array
  • Encode RPC object into a Uint8Array. This can be override to use a custom router protobuf.

    Parameters

    Returns Uint8Array

Protected _onIncomingStream

  • _onIncomingStream(__namedParameters: { connection: Connection; protocol: string; stream: MuxedStream }): void

Protected _onPeerConnected

  • Registrar notifies an established connection with pubsub protocol.

    Parameters

    Returns Promise<void>

Protected _onPeerDisconnected

  • _onPeerDisconnected(peerId: PeerId, err?: Error | undefined): void
  • Registrar notifies a closing connection with pubsub protocol.

    Parameters

    • peerId: PeerId

      peerId

    • Optional err: Error | undefined

    Returns void

_processMessages

  • _processMessages(idB58Str: string, stream: AsyncIterable<Uint8Array | BufferList>, peerStreams: PeerStreams): Promise<void>
  • Responsible for processing each RPC message received by other peers.

    Parameters

    • idB58Str: string

      peer id string in base58

    • stream: AsyncIterable<Uint8Array | BufferList>

      inbound stream

    • peerStreams: PeerStreams

      PubSub peer

    Returns Promise<void>

_processRpc

  • _processRpc(idB58Str: string, peerStreams: PeerStreams, rpc: RPC): boolean
  • Handles an rpc request from a peer

    Parameters

    Returns boolean

_processRpcMessage

  • _processRpcMessage(msg: InMessage): Promise<void>
  • Handles an message from a peer

    Parameters

    Returns Promise<void>

_processRpcSubOpt

  • _processRpcSubOpt(id: string, subOpt: RPCSubOpts): void
  • Handles a subscription change from a peer

    Parameters

    Returns void

_publish

  • _publish(message: InMessage): Promise<void>
  • Overriding the implementation of publish should handle the appropriate algorithms for the publish/subscriber implementation. For example, a Floodsub implementation might simply publish each message to each topic for every peer

    abstract

    Parameters

    Returns Promise<void>

Protected _removePeer

  • Notifies the router that a peer has been disconnected.

    Parameters

    Returns PeerStreams | undefined

_sendRpc

  • _sendRpc(id: string, rpc: RPC): void
  • Send an rpc object to a peer

    Parameters

    • id: string

      peer id

    • rpc: RPC

    Returns void

_sendSubscriptions

  • _sendSubscriptions(id: string, topics: string[], subscribe: boolean): void
  • Send subscroptions to a peer

    Parameters

    • id: string

      peer id

    • topics: string[]
    • subscribe: boolean

      set to false for unsubscriptions

    Returns void

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): any
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns any

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

getMaxListeners

  • getMaxListeners(): number

getMsgId

  • The default msgID implementation Child class can override this.

    Parameters

    Returns Uint8Array

    message id as bytes

getSubscribers

  • getSubscribers(topic: string): Array<string>
  • Get a list of the peer-ids that are subscribed to one topic.

    Parameters

    • topic: string

    Returns Array<string>

getTopics

  • getTopics(): Array<string>
  • Get the list of topics which the peer is subscribed to.

    override

    Returns Array<string>

listenerCount

  • listenerCount(event: string | symbol): number

listeners

  • listeners(event: string | symbol): Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): any
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns any

on

  • on(event: string | symbol, listener: (...args: any[]) => void): any
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns any

once

  • once(event: string | symbol, listener: (...args: any[]) => void): any
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns any

publish

  • publish(topic: string, message: Uint8Array): Promise<void>
  • Publishes messages to all subscribed peers

    override

    Parameters

    • topic: string
    • message: Uint8Array

    Returns Promise<void>

rawListeners

  • rawListeners(event: string | symbol): Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): any

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): any
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns any

setMaxListeners

  • setMaxListeners(n: number): any

start

  • start(): void
  • Register the pubsub protocol onto the libp2p node.

    Returns void

stop

  • stop(): void
  • Unregister the pubsub protocol and the streams with other peers will be closed.

    Returns void

subscribe

  • subscribe(topic: string): void
  • Subscribes to a given topic.

    abstract

    Parameters

    • topic: string

    Returns void

unsubscribe

  • unsubscribe(topic: string): void
  • Unsubscribe from the given topic.

    override

    Parameters

    • topic: string

    Returns void

validate

  • validate(message: InMessage): Promise<void>
  • Validates the given message. The signature will be checked for authenticity. Throws an error on invalid messages

    Parameters

    Returns Promise<void>