These packages help different parts of your code communicate with each other using events. EventEmitter3 is a fast, simple way to handle events within your application, similar to Node.js's built-in EventEmitter. PubSub-js is more focused on the publish/subscribe pattern, where parts of your code can subscribe to topics and receive messages, making it great for loosely coupled applications.
Both packages provide event-driven communication, but eventemitter3 is more lightweight and flexible, while pubsub-js offers more advanced features like async support and error handling.
Both packages have TypeScript definitions, but eventemitter3 has better support for TypeScript 4.x.
Both packages are compatible with modern browsers, but eventemitter3 has better support for older browsers like IE11.
Both packages have no dependencies.
eventemitter3 is generally faster and more efficient due to its smaller size and simpler implementation.
Both packages are compatible with popular frameworks like React, Angular, and Vue.js.
eventemitter3 has a more active community with more contributors and issues resolved.
Both packages have good documentation, but eventemitter3 has more examples and a more detailed API reference.
eventemitter3 has a more frequent maintenance schedule with more recent updates.
1const EventEmitter = require('eventemitter3');
2const emitter = new EventEmitter();
3emitter.on('event', () => console.log('Event emitted!'));
4emitter.emit('event');
This example shows how to create an event emitter and emit an event using eventemitter3.
1const PubSub = require('pubsub-js');
2const token = PubSub.subscribe('event', () => console.log('Event received!'));
3PubSub.publish('event', 'Hello, world!');
This example shows how to use the publish-subscribe pattern with pubsub-js.
eventemitter3 is a better choice for most use cases due to its smaller size, better performance, and more active community.
A tiny event emitter library that's super simple to use. It's less than 200 bytes in size and has a very straightforward API for handling events in your JavaScript applications.
Perfect for developers who want a simpler, more lightweight alternative to eventemitter3. It does the same core job but with less code and fewer features, which makes it easier to learn and use.
Event EmitterA small but capable event emitter that works in both browser and Node.js environments. Offers the basic features you need for event handling without any extra complexity.
Works as a drop-in replacement for eventemitter3 in most cases, while being smaller and simpler. Good for developers who want something easy to understand but still reliable.
Event EmitterA powerful message bus library that helps manage communication between different parts of your application. It's like a more advanced version of pubsub-js with extra features.
Great alternative to pubsub-js when you need more advanced features like message channels and hierarchical topics. It's especially good for larger applications.
Pub/Sub LibraryA very small event emitter library focused on being as tiny as possible. It provides basic event handling capabilities with minimal overhead.
Great choice when you need the smallest possible event emitter that still works well. It's perfect for small projects or when bundle size is super important.
Event EmitterA comprehensive library for handling events and data streams. While it's bigger than pubsub-js, it offers many powerful tools for managing how data flows through your application.
Perfect when you need more than just basic pub/sub functionality. It's great for complex applications where you need to transform, combine, or filter events in advanced ways.
Reactive Programming LibraryEventEmitter3 is a high performance EventEmitter. It has been micro-optimized for various of code paths making this, one of, if not the fastest EventEmitter available for Node.js and browsers. The module is API compatible with the EventEmitter that ships by default with Node.js but there are some slight differences:
throw
an error when you emit an error
event and nobody is
listening.newListener
and removeListener
events have been removed as they
are useful only in some uncommon use-cases.setMaxListeners
, getMaxListeners
, prependListener
and
prependOnceListener
methods are not available.fn.bind
.removeListener
method removes all matching listeners, not only the
first.It's a drop in replacement for existing EventEmitters, but just faster. Free performance, who wouldn't want that? The EventEmitter is written in EcmaScript 3 so it will work in the oldest browsers and node versions that you need to support.
$ npm install --save eventemitter3
Recommended CDN:
https://unpkg.com/eventemitter3@latest/dist/eventemitter3.umd.min.js
After installation the only thing you need to do is require the module:
var EventEmitter = require('eventemitter3');
And you're ready to create your own EventEmitter instances. For the API documentation, please follow the official Node.js documentation:
http://nodejs.org/api/events.html
We've upgraded the API of the EventEmitter.on
, EventEmitter.once
and
EventEmitter.removeListener
to accept an extra argument which is the context
or this
value that should be set for the emitted events. This means you no
longer have the overhead of an event that required fn.bind
in order to get a
custom this
value.
var EE = new EventEmitter() , context = { foo: 'bar' }; function emitted() { console.log(this === context); // true } EE.once('event-name', emitted, context); EE.on('another-event', emitted, context); EE.removeListener('another-event', emitted, context);
This module is well tested. You can run:
npm test
to run the tests under Node.js.npm run test-browser
to run the tests in real browsers via Sauce Labs.We also have a set of benchmarks to compare EventEmitter3 with some available
alternatives. To run the benchmarks run npm run benchmark
.
Tests and benchmarks are not included in the npm package. If you want to play
with them you have to clone the GitHub repository.
Note that you will have to run an additional npm i
in the benchmarks folder
before npm run benchmark
.