pubsubjs-m4r

PubSub for node and the web

Usage no npm install needed!

<script type="module">
  import pubsubjsM4r from 'https://cdn.skypack.dev/pubsubjs-m4r';
</script>

README

PubSubJS

Build Status

Simple pubsub solution for node and web

Use

  • CDN

    • Production
      <script src="https://unpkg.com/pubsubjs-m4r/dist/umd/pubsubjs.prod.js"></script>
      
    • Development
      <script src="https://unpkg.com/pubsubjs-m4r/dist/umd/pubsubjs.dev.js"></script>
      
  • npm

    npm i -S pubsubjs-m4r
    
  • yarn

    yarn add pubsubjs-m4r
    

How to use

import PubSubJS from 'pubsubjs-m4r'
// Or
// const PubSubJS = require("pubsubjs-m4r").default

// Subscribe to topic "abc"
const sub = PubSubJS.subscribe('abc', data => {
    // Your code
    console.log(data)
})

// Publish to topic "acb"
PubSubJS.publish('abc', 'Hello')

// unsub!!
sub()

API

  • PubSubJS

    • Subscribe - subscribe to notifications on a given topic
    subscribe: (topic: string, cb: Action<any>) => UnsubAction
    
    • Publish - publishes to a given topic
    publish: (topic: string, data: any, sync?: boolean) => void;
    
    
    • ClearAllSubscriptions - removes all subscriptions
    clearAllSubscriptions: () => void;
    
    
    • ClearAllByTopic - removes all subscriptios on a topic
    clearAllByTopic: (topic: string) => void;
    
    
    • GetTopics - get the registered topics
    getTopics: () => string[];