ez-channel

Lightweight library to create custom events and trigger and subscribe them from everywhere. Works standalone or with other frameworks like react.js or vue.js ===============

Usage no npm install needed!

<script type="module">
  import ezChannel from 'https://cdn.skypack.dev/ez-channel';
</script>

README

Lightweight library to create custom events and trigger and subscribe them from everywhere. Works standalone or with other frameworks like react.js or vue.js

What can this program do ?

Sometimes you need a simple implementation to create global channels and subscribe them from other pieces of code, components or stuff like that. With ez-channel it is easy ;-)

Benefit

If you want to define, trigger and process events in different files, this library can help you. You only need to include ez-channel in your files where you want to work with your custom events.

Installation

Package is available here: https://www.npmjs.com/package/ez-channel

  npm i ez-channel

Usage

Just include it into your web or server project

node

  const Channel = require('ez-channel');

web

  import {Channel} from 'ez-channel';

or you can include the transpiled script from dist folder.

create channel

A channel is that object that manage all events and subscriptions for you. To create a new channel, just do it like follow.

  Channel.create("myAwesomeChannel");

subscribe

Subscribe gives you all the functions that you need to trigger and listen for events. See example below.

  const channel = Channel.subscribe();

create and listen to event

If you want to create an event, uhm let´s say "youAreAwesome" and you want listen to that, just do it like follow.

  channel.on("myAwesomeChannel", "youAreAwesome", function() {
      console.log("someone says your are awesome");
    });

dispatch event

If you want to dispatch an events, just simple do it like follow.

  channel.send("myAwesomeChannel", "youAreAwesome");

dispatch and listen with data

Of course it is possible the send and receive data by "on" and "send". See example below:

  channel.send("myAwesomeChannel", "youAreAwesome",{isAwesome: true});
  channel.on("myAwesomeChannel", "youAreAwesome", function(obj) {
      if(obj.isAwesome) {
        console.log("you are awesome");
      }
    });

remove channel

Maybe it make sometimes sense to remove a channel. You can do it like follow.

  channel.remove("myAwesomeChannel");

Licence

Apache 2.0

Happy using and keep coding =)