What is @socket.io/component-emitter?
@socket.io/component-emitter is a lightweight event emitter library that allows you to create and manage custom events. It is commonly used in applications where you need to implement an event-driven architecture, such as in real-time web applications.
What are @socket.io/component-emitter's main functionalities?
Event Emission
This feature allows you to emit custom events and listen for them. In the code sample, an event named 'event' is emitted, and a listener logs a message to the console when the event occurs.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
emitter.on('event', function() {
console.log('An event occurred!');
});
emitter.emit('event');
Event Removal
This feature allows you to remove specific event listeners. In the code sample, the listener for the 'event' is removed before the event is emitted, so no message is logged to the console.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
function response() {
console.log('An event occurred!');
}
emitter.on('event', response);
emitter.off('event', response);
emitter.emit('event');
Once Event Listener
This feature allows you to add a listener that will be called at most once for a particular event. In the code sample, the listener logs a message only the first time the 'event' is emitted.
const Emitter = require('@socket.io/component-emitter');
const emitter = new Emitter();
emitter.once('event', function() {
console.log('This will only log once');
});
emitter.emit('event');
emitter.emit('event');
Other packages similar to @socket.io/component-emitter
events
The 'events' package is the built-in Node.js module for handling events. It provides a similar event-driven architecture and is widely used in Node.js applications. Compared to @socket.io/component-emitter, 'events' is more feature-rich and is part of the Node.js core.
eventemitter3
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to @socket.io/component-emitter in terms of functionality but is optimized for performance and has a smaller footprint.
mitt
The 'mitt' package is a tiny (~200 bytes) functional event emitter. It provides a minimal API for event handling and is suitable for use in both Node.js and browser environments. Compared to @socket.io/component-emitter, 'mitt' is more lightweight and has a simpler API.
Emitter ![Build Status](https://travis-ci.org/component/emitter.png)
Event emitter component.
Installation
$ component install component/emitter
API
Emitter(obj)
The Emitter
may also be used as a mixin. For example
a "plain" object may become an emitter, or you may
extend an existing prototype.
As an Emitter
instance:
var Emitter = require('emitter');
var emitter = new Emitter;
emitter.emit('something');
As a mixin:
var Emitter = require('emitter');
var user = { name: 'tobi' };
Emitter(user);
user.emit('im a user');
As a prototype mixin:
var Emitter = require('emitter');
Emitter(User.prototype);
Emitter#on(event, fn)
Register an event
handler fn
.
Emitter#once(event, fn)
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
Emitter#off(event, fn)
- Pass
event
and fn
to remove a listener. - Pass
event
to remove all listeners on that event. - Pass nothing to remove all listeners on all events.
Emitter#emit(event, ...)
Emit an event
with variable option args.
Emitter#listeners(event)
Return an array of callbacks, or an empty array.
Emitter#hasListeners(event)
Check if this emitter has event
handlers.
License
MIT