Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
event-pubsub
Advanced tools
Super light and fast Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions. Easy for any developer level. No frills, just high speed pub
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.
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);
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 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 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.
npm info :
GitHub info :
Super light and fast Extensible PubSub events and EventEmitters for Node and the browser with support for ES6 by default, and ES5 versions for older verions of node and older IE/Safari versions.
Easy for any developer level. No frills, just high speed events following the publisher subscriber pattern!
See NPM stats for event-pubsub
EXAMPLE FILES
Node Install
npm i --save event-pubsub
By default the ES6 version will be used. you can use the es5 version for older versions of node by requiring event-pubsub/es5.js
.
Browser Install
see browser examples above or below
<script src='path/to/event-pubsub-browser.js'></script>
<!-- OR ES5 for older browser support
<script src='path/to/event-pubsub-browser-es5.js'></script>
-->
Method | Arguments | Description |
---|---|---|
subscribe | type (string), handler(function) | will bind the handler function to the the type event. Just like addEventListener in the browser |
on | same as above | same as above |
unSubscribe | type (string), handler(function or *) | will unbind the handler function from the the type event. If the handler is * , all handlers for the event type will be removed. Just like removeEventListener in the browser, but also can remove all event handlers for the type. |
off | same as above | same as above |
publish | type (string), ...data arguments | will call all handler functions bound to the event type and pass all ...data arguments to those handlers |
emit | same as above | same as above |
trigger | same as above | same as above |
While publish
, subscribe
, and unSubscribe
are the proper method names for the publisher/subscriber model, we have included on
, off
, and emit
as well because these are the most common event method names, and shorter. We have also kept the trigger
method as an alias for publish
and emit
for backwards compatability with earlier versions of event-pubsub
.
*
typeThe *
type is a special event type that will be triggered by any publish or emit the handlers for these should expect the first argument to be the type and all arguments after to be data arguments.
NOTE - the only difference between node and browser code is how the events
variable is created
const events = new Events
const events = new window.EventPubSub;
const Events = new require('event-pubsub');
const events=new Events;
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
events.emit(
'world',
{
type:'myObject',
data:{
x:'YAY, Objects!'
}
}
)
}
);
events.on(
'world',
function(data){
console.log('World event got',data);
events.off('*','*');
console.log('Removed all events');
}
);
events.emit(
'hello',
'world'
);
events.on(
'hello',
someFunction
).on(
'goodbye',
anotherFunction
).emit(
'hello',
'world'
);
events.emit(
'goodbye',
'complexity'
).off(
'hello',
'*'
);
<!DOCTYPE html>
<html>
<head>
<title>PubSub Example</title>
<script src='../../event-pubsub-browser.js'></script>
<!-- OR ES5 for older browser support
<script src='../../event-pubsub-browser-es5.js'></script>
-->
<script src='yourAmazingCode.js'></script>
</head>
<body>
...
</body>
</html>
var events = new window.EventPubSub();
events.on(
'hello',
function(data){
console.log('hello event recieved ', data);
events.emit(
'world',
{
type:'myObject',
data:{
x:'YAY, Objects!'
}
}
)
}
);
events.emit(
'hello',
'world'
);
events.emit(
'hello',
'again','and again'
);
const Events = require('event-pubsub');
class Book extends Events{
constructor(){
super();
//now Book has .on, .off, and .emit
this.words=[];
}
add(...words){
this.words.push(...words);
this.emit(
'added',
...words
);
}
read(){
this.emit(
'reading'
);
console.log(this.words.join(' '));
}
}
const book=new Book;
book.on(
'added',
function(...words){
console.log('words added : ',words);
this.read();
}
);
book.add(
'once','upon','a','time','in','a','cubicle'
);
const Events = require('event-pubsub/es5.js');
function Book(){
//extend happens below
Object.assign(this,new Events);
//now Book has .on, .off, and .emit
this.words=[];
this.add=add;
this.erase=erase;
this.read=read;
function add(){
arguments.slice=Array.prototype.slice;
this.words=this.words.concat(
arguments.slice()
);
this.emit(
'added',
arguments.slice()
);
}
function read(){
this.emit(
'reading'
);
console.log(this.words.join(' '));
}
return this;
};
const book=new Book;
book.on(
'added',
function(...words){
console.log('words added : ',words);
this.read();
}
);
book.add(
'once','upon','a','time','in','a','cubicle'
);
FAQs
Super light and fast Extensible ES6+ events and EventEmitters for Node and the browser. Easy for any developer level, use the same exact code in node and the browser. No frills, just high speed events!
We found that event-pubsub demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.