Send a message to n subscribed users when a date / time approaches nodejs

3

I want to be able to send a message to a user via email or sms, given a date and time for example 10/24/2016 14:00:00 send a message to you one hour before, I am using nodemailer to send via email, and twilio to send via sms, but the question is how do I program the event to do what I want, a user to register an event, and from that event I want all those who are subscribed to that event, to receive a notification when the event is about 1hs before. Someone who had done something similar, I would appreciate your help.

Update I use redis to store sessions, and maybe I can also take advantage of redis to use cron-cluster. Has anyone tried this module, and how has it gone?

    
asked by Kevin AB 14.10.2016 в 21:58
source

1 answer

2

With this code you can execute a function at a certain time (every day):

"use strict";

function tarea(){
    console.log('acá va la tarea', new Date());
}

function lanzarSiempreALaHora(hora, minutos, tarea){
    var ahora = new Date();
    console.log('lanzado',ahora);
    var momento = new Date(ahora.getFullYear(), ahora.getMonth(), ahora.getDate(), hora, minutos);
    if(momento<=ahora){ // la hora era anterior a la hora actual, debo sumar un día
        momento = new Date(momento.getTime()+1000*60*60*24);
    }
    console.log('para ser ejecutado en',momento,momento.getTime()-ahora.getTime());
    setTimeout(function(){
        tarea();
        lanzarSiempreALaHora(hora,minutos,tarea);
    },momento.getTime()-ahora.getTime());
}

lanzarSiempreALaHora(21,10, tarea);

The lanzarSiempreALaHora function is the one that adds tasks / functions. I understand that you already know how to make a function that sends mails or SMS.

Notes

  • A problem may occur if the machine where the program is running has a time change. If that could happen, this simplified version would not work (the first time I would execute the task at the wrong time).
  • The task can be parameterized by passing an anonymous function and using the parameters to set the values (that is only necessary within a cycle):
  • example:

    lanzarSiempreALaHora(10, 30, function(nombre, mail){
        envair_mail(nombre, mail);
    }(nombre, mail));
    
        
    answered by 16.10.2016 в 02:18