Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Event Bus for Node.JS using Bull Queues
npm install bull-bus
When we work with event buses we normally have 1 event that can be consumed by N subscribers. When we want to create a new subscriber we will need to provide 3 main things:
Topic Name
: will be used to know the subscriptions that should be executed when a new topic is published.Subscriber Name
: we can have N subscribers to a topic. The pair (topicName, subscriberName) will identify a unique subscription. Check how this is useful to visualize the queues.Handler
: this is the function that will be executed when an event is published to a particular topic.Bull Bus library offers two main functionalities. The bull bus and the bull event bus.
This class is a Bus Implementation using Bull, works with primitives data and does not know anything about the domain. It may be useful in case we want to build our own domain event logic.
// Topic Name creations
const accountCreatedTopicName = "account-created";
const userCreatedTopicName = "user-created";
// Subscriber name creations
const sendEmailSubscriberName = "send-email";
const sendSlackSubscriberName = "send-slack";
const sendPushNotificationSubscriberName = "send-push-notification";
const bullBus = new BullBus({
redisUrl: "redis://127.0.0.1:6379",
topicNameToSubscriberNames: {
[accountCreatedTopicName]: [
sendEmailSubscriberName,
sendSlackSubscriberName,
],
[userCreatedTopicName]: [sendPushNotificationSubscriberName],
},
});
bullBus.addSubscribers([
{
topicName: accountCreatedTopicName,
handleEvent: async (payload: unknown) => {
console.log("Handle Event Topic A, Handler 1 ", payload);
},
subscriberName: sendEmailSubscriberName,
},
{
topicName: accountCreatedTopicName,
handleEvent: async (payload: unknown) => {
console.log("Handle Event Topic A, Handler 2 ", payload);
},
subscriberName: sendSlackSubscriberName,
},
{
topicName: userCreatedTopicName,
handleEvent: async (payload: unknown) => {
console.log("payload handler B: ", payload);
},
subscriberName: sendPushNotificationSubscriberName,
},
]);
await bullBus.publish(accountCreatedTopicName, {
accountId: "2",
});
await bullBus.publish(userCreatedTopicName, {
userId: "1",
});
Bull Event Bus is very similar to the Bull Bus with the difference that gives us some default classes to create domain events and subscriptions. Its useful when we are working with OOP.
class UserRegistered extends DomainEvent {
static EVENT_NAME = "user-registered";
constructor(userName: string) {
super({
eventName: UserRegistered.EVENT_NAME,
attributes: {
userName,
},
});
}
}
class UserFormCompleted extends DomainEvent {
static EVENT_NAME = "user-form-completed";
constructor(value: string) {
super({
eventName: UserFormCompleted.EVENT_NAME,
attributes: {
value,
},
});
}
}
class SendSlackOnUserOrFormCompleted
implements DomainEventSubscriber<UserRegistered | UserFormCompleted>
{
subscribedTo() {
return [UserRegistered, UserFormCompleted];
}
subscriberName(): string {
return "send-slack";
}
async on(event: UserRegistered | UserFormCompleted) {
switch (event.eventName) {
case UserRegistered.EVENT_NAME:
console.log("Simulating send slack...", event.attributes.userName);
break;
case UserFormCompleted.EVENT_NAME:
console.log("Simulating send slack...", event.attributes.value);
break;
}
}
}
class SendEmailOnUserRegistered
implements DomainEventSubscriber<UserRegistered>
{
subscribedTo() {
return [UserRegistered];
}
subscriberName(): string {
return "send-email";
}
async on(event: UserRegistered) {
console.log("Simulating send email...", event.attributes.userName);
}
}
const eventBus = new BullEventBus({
redisUrl: "redis://127.0.0.1:6379",
topicNameToSubscriberNames: {
[UserRegistered.EVENT_NAME]: ["send-slack", "send-email"],
[UserFormCompleted.EVENT_NAME]: ["send-slack"],
},
});
eventBus.addSubscribers([
new SendSlackOnUserOrFormCompleted(),
new SendEmailOnUserRegistered(),
]);
await eventBus.publish([new UserRegistered("gabriel")]);
await eventBus.publish([new UserFormCompleted("3208")]);
Both buses are ready to show the internal queues to display the job data in a pretty way. The following image is using Taskforce, but can be used any UI for Bull.
This library offers a playground where we can play with the functions that we are developing
docker-compose up -d redis
npm run playground
This library has been designed to work with node v16 and npm 8. In order to configure your local environment you can run:
nvm install 16.0.0
nvm use
npm install npm@8.3.0 -g
npm install
npm run build
npm run test
Run the linter
npm run lint
Fix lint issues automatically
npm run lint:fix
Contributions welcome! See the Contributing Guide.
FAQs
Event Bus for Node.JS using Bull Queues
The npm package bull-bus receives a total of 18 weekly downloads. As such, bull-bus popularity was classified as not popular.
We found that bull-bus demonstrated a not healthy version release cadence and project activity because the last version was released 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
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.