What is event-pubsub?
The event-pubsub package is a Node.js module that provides a simple interface for implementing the publish-subscribe pattern. This pattern allows for the creation of a decoupled system where publishers and subscribers can communicate through events without being directly connected.
What are event-pubsub's main functionalities?
Event Emission
This feature allows you to emit events with a specified name and pass data to the event listeners. In the code sample, an event listener is set up to listen for the 'sayHello' event and emit that event with a parameter.
const EventPubSub = require('event-pubsub');
const events = new EventPubSub();
// Event listener
events.on('sayHello', name => console.log(`Hello, ${name}!`));
// Emit event
events.emit('sayHello', 'World');
Event Listening
This feature allows you to listen for events by name. When the event is emitted, the callback function is called with any data passed to the event. In the code sample, an event listener is set up to listen for the 'dataReceived' event.
const EventPubSub = require('event-pubsub');
const events = new EventPubSub();
// Event listener
events.on('dataReceived', data => console.log(`Data: ${data}`));
Once Listeners
This feature allows you to set up event listeners that will only be triggered once. After the event is emitted and the listener is invoked, the listener is removed. In the code sample, an event listener is set up to listen for the 'initialize' event and will only be triggered once.
const EventPubSub = require('event-pubsub');
const events = new EventPubSub();
// Event listener that fires once
events.once('initialize', () => console.log('Initialization complete.'));
Removing Listeners
This feature allows you to remove specific event listeners. In the code sample, an event listener is added and then removed for the 'message' event.
const EventPubSub = require('event-pubsub');
const events = new EventPubSub();
function onMessage(content) {
console.log(`Message: ${content}`);
}
// Add and remove an event listener
events.on('message', onMessage);
events.off('message', onMessage);
Other packages similar to event-pubsub
eventemitter3
eventemitter3 is a high performance EventEmitter. It provides similar functionality to event-pubsub but focuses on performance and is compatible with different module systems (CommonJS, AMD, and browser globals).
mitt
mitt is a tiny functional event emitter / pubsub. It offers a similar publish-subscribe pattern but with a smaller footprint, making it a good choice for front-end development and projects where size is a concern.
wolfy87-eventemitter
wolfy87-eventemitter is an EventEmitter implementation that provides asynchronous event emitting and namespaced events. It is more feature-rich compared to event-pubsub, offering additional functionality such as context binding and wildcard events.
Event PubSub
npm info :
GitHub info :
Pubsub events for Node and the browser allowing event scoping and multiple scopes.
Easy for any developer level. No frills, just high speed pubsub events!
Pretty GitHub.io site
See NPM stats for event-pubsub
EXAMPLE FILES
- Node Pubsub Event Examples
- Browser Pubsub Event Examples
Node Install
npm install event-pubsub
Browser Install
see browser examples above or below
Basic Example
NOTE - the only diffeence between node and browser code is how the events
variable is created
- node
var events = new require('../../event-pubsub.js')();
- browser
var events = new window.pubsub();
Node
var events = new require('../../event-pubsub.js')();
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
}
);
events.on(
'*',
function(type){
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
}
);
events.on(
'removeEvents',
function(){
events.off('*');
console.log('Removed all events');
}
);
/************************************\
* trigger events for testing
* **********************************/
events.trigger(
'hello',
'world'
);
events.trigger(
'removeEvents'
);
Browser
HTML
<!DOCTYPE html>
<html>
<head>
<title>PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
<script src='yourAmazingCode.js'></script>
</head>
<body>
...
</body>
</html>
Inside Your Amazing Code
var events = new window.pubsub();
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
}
);
events.on(
'*',
function(type){
console.log('Catch all detected event type of : ',type, '. List of all the sent arguments ',arguments);
}
);
events.on(
'removeEvents',
function(){
events.off('*');
console.log('Removed all events');
}
);
/************************************\
* trigger events for testing
* **********************************/
events.trigger(
'hello',
'world'
);
events.trigger(
'removeEvents'
);