Type Parameters

Hierarchy

Properties

#private: any
getPeers: (() => PeerId[])

Type declaration

getSubscribers: ((topic: string) => PeerId[])

Type declaration

    • (topic: string): PeerId[]
    • Gets a list of the PeerIds that are subscribed to one topic.

      Example

      const peerIds = libp2p.pubsub.getSubscribers(topic)
      

      Parameters

      • topic: string

      Returns PeerId[]

getTopics: (() => string[])

Type declaration

    • (): string[]
    • Gets a list of topics the node is subscribed to.

      const topics = libp2p.pubsub.getTopics()
      

      Returns string[]

globalSignaturePolicy: "StrictSign" | "StrictNoSign"

The global signature policy controls whether or not we sill send and receive signed or unsigned messages.

Signed messages prevent spoofing message senders and should be preferred to using unsigned messages.

multicodecs: string[]

A list of multicodecs that contain the pubsub protocol name.

publish: ((topic: string, data: Uint8Array) => Promise<PublishResult>)

Type declaration

subscribe: ((topic: string) => void)

Type declaration

    • (topic: string): void
    • Subscribes to a pubsub topic.

      Example

      const topic = 'topic'
      const handler = (msg) => {
      if (msg.topic === topic) {
      // msg.data - pubsub data received
      }
      }

      libp2p.pubsub.addEventListener('message', handler)
      libp2p.pubsub.subscribe(topic)

      Parameters

      • topic: string

      Returns void

topicValidators: Map<string, TopicValidatorFn>

Pubsub routers support message validators per topic, which will validate the message before its propagations. They are stored in a map where keys are the topic name and values are the validators.

Example

const topic = 'topic'
const validateMessage = (msgTopic, msg) => {
const input = uint8ArrayToString(msg.data)
const validInputs = ['a', 'b', 'c']

if (!validInputs.includes(input)) {
throw new Error('no valid input received')
}
}
libp2p.pubsub.topicValidators.set(topic, validateMessage)
unsubscribe: ((topic: string) => void)

Type declaration

    • (topic: string): void
    • Unsubscribes from a pubsub topic.

      Example

      const topic = 'topic'
      const handler = (msg) => {
      // msg.data - pubsub data received
      }

      libp2p.pubsub.removeEventListener(topic handler)
      libp2p.pubsub.unsubscribe(topic)

      Parameters

      • topic: string

      Returns void

Methods

  • Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • listener: null | EventHandler<Events[K]>
    • Optional options: boolean | AddEventListenerOptions

    Returns void

  • Parameters

    • type: string

    Returns number

  • Type Parameters

    • K extends string | number | symbol

    Parameters

    • type: K
    • Optional listener: null | EventHandler<Events[K]>
    • Optional options: boolean | EventListenerOptions

    Returns void

  • Type Parameters

    • Detail

    Parameters

    • type: keyof Events
    • detail: CustomEventInit<Detail>

    Returns boolean