DEV Community

Ben Sooraj
Ben Sooraj

Posted on

Ossa: A node.js server-side module (powered by Redis) for sending scheduled messages

Disclaimer:

  1. This is a side project! It is by no way complete, but it does get the job done.
  2. This module is not production ready!
  3. And is good for small projects/apps

At my work, I had to solve a problem where we send reminders to our users nudging them to prioritise a task pending with them (can't go beyond this with the details). These reminders were required to be configurable, that is, users get to choose when they want to be reminded: in 2 days, in 1 hour on a specific date and time etc.

After quite a bit of digging around the internet, I stumbled upon Redis Keyspace Notifications. There are many other ways you could implement this (cron jobs for example), but I decided to go with this approach. And since Redis was already one of our tech stacks, it was a viable option worth trying out.

I went ahead and wrapped up this feature into a module called Ossa.

Pretty easy to get started.

Create an ossa instance:

const Ossa = require('ossa');
const ossa = new Ossa({
    namespace: "ossa", // Default
    redis: {
        host: 'localhost', // Default
        port: 6379 // Default
    },
    debug: false, // Default
    mode: 0  // 0 => Send and receive (Default) | 1 => Send only
});

To schedule a notification/message:

try {
    const notificationID = await ossa.sendNotification({
        in: '10s',
        // on: moment().utc().add(30, 'seconds'),
        // on: '2020-05-02 03:23:00',
        // on: '2020-05-01T21:59:16Z',
        message: JSON.stringify({
            name: "Ben",
            age: 1000,
        })
    });
    console.log("notificationID: ", notificationID)
} catch (error) {
    throw new Error(error);
}

// Output:
// notificationID:  ossa::f1799e87-6740-4394-bf5e-d6e55eae3914

To receive the scheduled message:

ossa.on('notification-received', async (notificationID, notificationPayload) => {
    // Process the payload received
    console.log("notificationPayload: ", notificationPayload)
    console.table([
        { notificationID, message: notificationPayload.message }
    ]);
});

// Output:
// notificationPayload:  { in: '10s', message: '{"name":"Ben","age":1000}' }
// ┌─────────┬──────────────────────────────────────────────┬─────────────────────────────┐
// │ (index) │                notificationID                │           message           │
// ├─────────┼──────────────────────────────────────────────┼─────────────────────────────┤
// │    0    │ 'ossa::f1799e87-6740-4394-bf5e-d6e55eae3914' │ '{"name":"Ben","age":1000}' │
// └─────────┴──────────────────────────────────────────────┴─────────────────────────────┘

Do checkout the README page to learn more.

I have also created a sample ready-to-run example using docker so you can get started even faster.

So, go ahead and check it out and let me know your thoughts. Would love some community feedback.

Thanks!

Top comments (0)