Interface PubSub<Events>

interface PubSub<Events> {
    globalSignaturePolicy: "StrictSign" | "StrictNoSign";
    multicodecs: string[];
    topicValidators: Map<string, TopicValidatorFn>;
    addEventListener<K>(type, listener, options?): void;
    dispatchEvent(event): boolean;
    dispatchEvent(event): boolean;
    getPeers(): PeerId[];
    getSubscribers(topic): PeerId[];
    getTopics(): string[];
    listenerCount(type): number;
    publish(topic, data): Promise<PublishResult>;
    removeEventListener<K>(type, listener?, options?): void;
    removeEventListener(type, listener?, options?): void;
    safeDispatchEvent<Detail>(type, detail): boolean;
    subscribe(topic): void;
    unsubscribe(topic): void;
}

Type Parameters

Hierarchy (view full)

Implemented by

    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.

    multicodecs: 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.

    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)

    Methods

    • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

      MDN Reference

      Parameters

      Returns boolean

    • Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

      MDN Reference

      Parameters

      Returns boolean

    • Subscribes to a pubsub topic.

      Parameters

      • topic: string

      Returns void

      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)
    • Unsubscribes from a pubsub topic.

      Parameters

      • topic: string

      Returns void

      Example

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

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