Get started with pubsub-js CDN

MIT licensed

PubSub-JS is a lightweight, publish-subscribe messaging library for JS applications.

Tags:
  • pub/sub
  • pubsub
  • publish/subscribe
  • publish
  • subscribe

Stable version

Copied!

How to start using pubsub-js CDN


<!DOCTYPE html>
<html>
<head>
  <title>Get started with pubsub-js CDN - cdnhub.io</title>
  <script src="https://cdn.cdnhub.io/pubsub-js/1.9.4/pubsub.min.js"></script>
</head>
<body>
  <button id="publish">Publish</button>
  <button id="subscribe">Subscribe</button>
  <div id="output"></div>

  <script>
    const pubsub = new PubSub();

    const publishButton = document.getElementById('publish');
    const subscribeButton = document.getElementById('subscribe');
    const output = document.getElementById('output');

    publishButton.addEventListener('click', () => {
      pubsub.publish('message', 'Hello, PubSub.js!');
      output.textContent += 'Published a message: ';
    });

    subscribeButton.addEventListener('click', () => {
      const messageHandler = (message) => {
        output.textContent += `Received message: ${message}\n`;
      };

      pubsub.subscribe('message', messageHandler);
      output.textContent += 'Subscribed to messages: ';
    });
  </script>
</body>
</html>
Copied!
Copied!

All versions