Options
All
  • Public
  • Public/Protected
  • All
Menu

Publisher provides a class for creating new publisher/subscriber relations.

import { Publisher } from '@kettek/publisher'

let publisher = new Publisher()

let subscriber = publisher.subscribe('topic.golang', async ({topic: string, message: any}) => {
console.log('received', message, 'on', topic)
})

// Also subscribe to topic.unix
publisher.subscribe('topic.unix', subscriber)

let subscriberAll = publisher.subscribe('topic.*', async ({topic: string, message: any}) => {
console.log('received', message, 'on', topic)
})

publisher.publish('topic.golang', 'hello from golang')
publisher.publish('topic.*', 'hello from *')
publisher.publish('topic.deno', 'hello from deno')

// subscriber will receive "hello from *" and "hello from golang"
// subscriberAll will receive all messages.

publisher.unsubscribe(subscriberAll)
publisher.unsubscribe('topic.golang', subscriber) // unsubscribe from golang, but retain unix subscription.

Hierarchy

  • Publisher

Index

Constructors

constructor

Properties

Private endpoints

endpoints: Map<Topic, Endpoint[]> = ...

A map of topics to endpoints

Private topics

topics: Map<Topic, Subscriber[]> = ...

A map of topics to subscribers.

Methods

connect

  • This creates or uses an endpoint for a topic and returns it.

    let endpoint = publisher.connect('*', async (msg: EndpointMessage): Promise<number> => {
    return await conn.send(msg) // assume `conn` has a matching endpoint on the other side.
    })

    Parameters

    Returns Endpoint

    The endpoint

  • This creates an endpoint for a topic.

    let endpoint = publisher.connect('*')

    endpoint.outbound = async (msg: EndpointMessage): Promise<number> => {
    return await conn.send(msg)
    }

    Parameters

    Returns Endpoint

    A new endpoint with a default handler.

disconnect

Private getMatchingTopics

Private getTopicEndpoints

Private getTopicSubscribers

Private isSubscribed

  • Checks if the subscriber is subscribed to a particular topic.

    For example, if a subscriber has been subscribed to topic.* and topic.golang is checked for, then this will return true.

    Parameters

    • subscriber: Subscriber

      The subscriber to check

    • topic: Topic

      The topic to check for

    Returns boolean

    Whether or not the subscriber is subscribed to the topic

Private matchTopic

publish

  • Sends a message to all subscribers and endpoints of a Topic.

    publisher.publish('topic.*', 'this is a message to subscribers of all topics')
    publisher.publish('topic.golang', 'this is a message to subscribers of topic.golang (and topic.* subscribers!)')
    throws

    PublishErrors if any subscribers or endpoints threw. Thrown after all subscribers have been messaged.

    Parameters

    Returns Promise<number>

    The number of subscribers who received the message.

  • Publishes a message to all subscribers and endpoints on behalf of a subscriber. The topic must be one the subscriber is subscribed to. The received publish message will contain fromSubscriber.

    publisher.publish(subscriber, 'topic.golang', 'Greetings from a subscriber')
    
    throws

    SubscriberPublishError if the subscriber sends to a topic it is not subscribed to.

    throws

    PublishErrors if any subscribers or endpoints threw. Thrown after all subscribers have been messaged.

    Parameters

    • subscriber: Subscriber

      The subscriber to send on behalf of.

    • topic: Topic

      A topic the subscriber is subscribed to.

    • message: any

      The message.

    Returns Promise<number>

  • Sends a message to all subscribers and endpoints, excluding the provided endpoint.

    publisher.publish(endpoint, message)
    
    throws

    EndpointPublishError if the provided message is not an EndpointMessage.

    throws

    PublishErrors if any subscribers or endpoints threw. Thrown after all subscribers have been messaged.

    Parameters

    • endpoint: Endpoint

      The endpoint to send on behalf of.

    • message: EndpointMessage

      An EndpointMessage for forwarding. The endpoint will never see this message.

    Returns Promise<number>

subscribe

  • Subscribes a handler to a topic and returns a new, corresponding Subscriber.


    publisher.subscribe('topic.golang', async ({topic, message}) => {
    console.log('handled', topic, message)
    })

    Parameters

    Returns Subscriber

    A new subscriber containing the handler.

  • Subscribes a subscriber to a given topic.

    let subscriber = publisher.subscribe('topic.golang')

    publisher.subscribe('topic.unix', subscriber)

    Parameters

    Returns Subscriber

    The subscriber passed in.

  • Creates a subscriber without a handler.

    let subscriber = publisher.subscribe('topic.golang')

    subscriber.handler = async ({topic, message}) => {
    console.log(message)
    }

    Parameters

    Returns Subscriber

unsubscribe

  • Unsubscribes a subscriber from a specific Topic pattern.

    publisher.unsubscribe('topic.golang', subscriber) // unsubscribes from a single topic.
    
    publisher.unsubscribe('topic.deno', handler)
    
    throws

    TypeError if the arguments are not as expected.

    Parameters

    Returns number

    The number of subscriptions unsubscribed.

  • Unsubscribes a subscriber from all topics it is subscribed to.

    publisher.unsubscribe(subscriber) // unsubscribes a subsciber from all topics.
    
    throws

    TypeError if the arguments are not as expected.

    Parameters

    Returns number

    The number of subscriptions unsubscribed.

Generated using TypeDoc