
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@socket.io/component-emitter
Advanced tools
@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.
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');
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.
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.
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.
@socket.io/component-emitter
Event emitter component.
This project is a fork of the component-emitter
project, with Socket.IO-specific TypeScript typings.
$ npm i @socket.io/component-emitter
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:
import { Emitter } from '@socket.io/component-emitter';
var emitter = new Emitter;
emitter.emit('something');
As a mixin:
import { Emitter } from '@socket.io/component-emitter';
var user = { name: 'tobi' };
Emitter(user);
user.emit('im a user');
As a prototype mixin:
import { Emitter } from '@socket.io/component-emitter';
Emitter(User.prototype);
Register an event
handler fn
.
Register a single-shot event
handler fn
,
removed immediately after it is invoked the
first time.
event
and fn
to remove a listener.event
to remove all listeners on that event.Emit an event
with variable option args.
Return an array of callbacks, or an empty array.
Check if this emitter has event
handlers.
MIT
FAQs
Event emitter
The npm package @socket.io/component-emitter receives a total of 5,214,331 weekly downloads. As such, @socket.io/component-emitter popularity was classified as popular.
We found that @socket.io/component-emitter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.