redismb

1.0.3 • Public • Published

redismb

npm npm GitHub license npm

Welcome to redismb! 🚀

This guide will help you get started with setting up a Redis message broker system based on Redis Streams using this powerful tool. Below are step-by-step instructions on initializing the Redis connection, creating subscribers and publishers, handling rejected messages, and more.

[!NOTE] Make sure to replace placeholders such as 'channel', 'group', 'action', and data with your actual channel names, consumer groups, actions, and data objects.

Installation

You can install the redismb library using npm:

npm install redismb

Getting Started

Now let's dive into how you can use the redismb library in your Node.js applications.

Initialize the Redis Connection

To start using the library, you need to initialize the Redis connection. Here's how you can do it:

import redismb, { Subscriber, Publisher } from 'redismb';
await redismb.bootstrap('redis://localhost:6379');

Create a Subscriber

Subscribers listen for messages on specific channels and consume them while assigned to a consumer group. You can create a subscriber instance like this:

const subscriber = new Subscriber({ channels: ['channel'], group: 'group' });
subscriber.subscribe(async ({ channel, action, data, id }) => {
  // Process the received message...
});

Create a Publisher

Publishers send messages to specific channels. Here's how you can create a publisher instance and publish a message:

const publisher = new Publisher({ channel: 'channel' });
const data = { foo: 'bar' };
await publisher.publish('action', data);

Unsubscribe and Stop the Redis Connection

When you're done with streaming messages, you can unsubscribe from channels and stop the Redis connection:

await subscriber.unsubscribe();
await redismb.stop();

Handle Rejected Messages

In case of rejected messages, you can read and reprocess them using the following methods:

await redismb.readRejectedMessages({ ids, from, to, action });
await redismb.reprocessRejectedMessages({ messages, ids, from, to, action });

Parameters:

  • ids (optional): An array of message IDs to filter rejected messages.
  • from (optional): The start date from which to filter rejected messages.
  • to (optional): The end date until which to filter rejected messages.
  • action (optional): Parameter used to filter rejected messages by a specific action.

If none of the previous parameters are defined, all messages will be retrieved.

  • messages(optional): Array of objects with optional properties: id, channel, group, and data. These properties are intended to republish the rejected messages (filtered by the previous parameters) to a different channel than they had, to a different consumer group from the one that revoked them, or with new properties in the data object.

Conclusion

That's it! You've learned how to set up a Redis message broker system using the redismb library. Feel free to explore more features and customize it according to your project's needs.

You can also find here a cool cheatsheet ❤️ about Redis Streams.

Happy messaging! 📨

Package Sidebar

Install

npm i redismb

Weekly Downloads

9

Version

1.0.3

License

MIT

Unpacked Size

43.5 kB

Total Files

16

Last publish

Collaborators

  • clara-jr