Node.js, date-time based reminder
Asked Answered
T

3

10

I am building a SaaS application, where users logs in and creates reminders for a particular date and time in future, and when the time is met, my application is responsible for reminding the respective user about the reminder he/she has set in the past

Example: Say user "Foo" logs in and creates a reminder on 2:30pm 27th April 2018 (a date time in future).

My application should send an email on that exact date-time to "Foo" as any other reminder app.

Like this there can be thousands of reminders ACTIVE for hundreds of users.

Technology

Node.js

What I tried

I have tried packages like node-schedule and other cron-job solutions but I believe/found - Cron-jobs are not designed for solving problems like one of mine.

Please help me with a solution. Thanks

Turmoil answered 26/4, 2017 at 8:55 Comment(2)
did u find the solution ?Lithia
Nope I did not find anyTurmoil
S
2

Not sure if you are still looking for a solution but I came across this :https://github.com/agenda/agenda

Its a lightweight node.js scheduling library.

Sissy answered 14/4, 2018 at 15:3 Comment(1)
The agenda seems to be using cronJobs under the hoodLenin
B
1

I will go with a redis based solution that sets ttl with bull that picks yp the task when time elapsed and sends the emails to the recipients.

  const express = require('express');
const bodyParser = require('body-parser');
const { Queue, Worker } = require('bull');
const nodemailer = require('nodemailer');

const app = express();
const port = 3000;

// In-memory storage for reminders (replace this with a database in production)
const remindersQueue = new Queue('reminders', { redis: { port: 6379, host: 'localhost' } });

app.use(bodyParser.json());

// Endpoint for creating reminders
app.post('/create-reminder', async (req, res) => {
  const { email, datetime, message } = req.body;

  const job = await remindersQueue.add({ email, message }, { delay: new Date(datetime) - new Date() });

  res.status(200).json({ success: true, message: 'Reminder created successfully', jobId: job.id });
});

// Set up a worker to process reminders
new Worker('reminders', async job => {
  const { email, message } = job.data;

  // Send email
  await sendEmail(email, 'Reminder', `Reminder: ${message}`);
});

// Function to send email using nodemailer (replace with your email service details)
async function sendEmail(to, subject, text) {
  const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: '[email protected]',
      pass: 'your-email-password',
    },
  });

  const mailOptions = {
    from: '[email protected]',
    to,
    subject,
    text,
  };

  return transporter.sendMail(mailOptions);
}

// Start the server
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
})

;

Breechblock answered 21/12, 2023 at 6:27 Comment(0)
C
0

If it's reminder by email, you should try SendGrid or Mailgun. They both offer a limited amount of emails for free. You can also try using Twilio for sms reminders. All can be set up pretty easily, you'd just have to invoke the api calls at set remind times.

Case answered 6/7, 2017 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.