libp2p
    Preparing search index...

    Interface GossipSub

    interface GossipSub {
        globalSignaturePolicy: "StrictSign" | "StrictNoSign";
        protocols: string[];
        topicValidators: Map<string, TopicValidatorFn>;
        addEventListener<K extends keyof GossipSubEvents>(
            type: K,
            listener: EventHandler<GossipSubEvents[K]> | null,
            options?: boolean | AddEventListenerOptions,
        ): void;
        dispatchEvent(event: Event): boolean;
        dispatchEvent(event: Event): boolean;
        getPeers(): PeerId[];
        getSubscribers(topic: string): PeerId[];
        getTopics(): string[];
        listenerCount(type: string): number;
        publish(
            topic: string,
            data?: Uint8Array<ArrayBufferLike>,
        ): Promise<PublishResult>;
        removeEventListener<K extends keyof GossipSubEvents>(
            type: K,
            listener?: EventHandler<GossipSubEvents[K]> | null,
            options?: boolean | EventListenerOptions,
        ): void;
        removeEventListener(
            type: string,
            listener?: EventHandler<Event>,
            options?: boolean | EventListenerOptions,
        ): void;
        safeDispatchEvent<Detail>(
            type: keyof GossipSubEvents,
            detail?: CustomEventInit<Detail>,
        ): boolean;
        subscribe(topic: string): void;
        unsubscribe(topic: string): void;
    }

    Hierarchy (View Summary)

    Index

    Properties

    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.

    protocols: string[]

    A list of multicodecs that contain the pubsub protocol name.

    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.

    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)

    Methods

    • The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

      MDN Reference

      Parameters

      Returns boolean

    • The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected event listeners in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent().

      MDN Reference

      Parameters

      Returns boolean

    • Gets a list of the PeerIds that are subscribed to one topic.

      Parameters

      • topic: string

      Returns PeerId[]

      const peerIds = libp2p.pubsub.getSubscribers(topic)
      
    • Gets a list of topics the node is subscribed to.

      const topics = libp2p.pubsub.getTopics()
      

      Returns string[]

    • Parameters

      • type: string

      Returns number

    • The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

      MDN Reference

      Type Parameters

      Parameters

      Returns void

    • The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target. The event listener to be removed is identified using a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal.

      MDN Reference

      Parameters

      • type: string
      • Optionallistener: EventHandler<Event>
      • Optionaloptions: boolean | EventListenerOptions

      Returns void

    • Subscribes to a pubsub topic.

      Parameters

      • topic: string

      Returns void

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

      libp2p.pubsub.addEventListener('message', handler)
      libp2p.pubsub.subscribe(topic)
    • Unsubscribes from a pubsub topic.

      Parameters

      • topic: string

      Returns void

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

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