Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@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.
Event emitter component.
$ component install 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:
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);
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 4,452,111 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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.