Ossa: A node.js server-side module (powered by Redis) for sending scheduled messages
Tags: node, webdev, redis, opensource
Published: 2020-05-12 17:37:39


*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**](https://redis.io/topics/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**](https://www.npmjs.com/package/ossa).

Pretty easy to get started.

Create an `ossa` instance:
```js
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:
```js
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:
```js
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](https://github.com/bensooraj/ossa#readme) page to learn more.

I have also created a sample [ready-to-run example](https://github.com/bensooraj/ossa/tree/master/examples) 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!