Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
Alternative Way Running a Timer on NodeJS Using Socket.IO

I want to create a reminder app. There should be a DB with table of 'active-reminders'. There would be a function which will:

  1. Identify which reminders reached their time
  2. Send a message to the client informing about their set reminder
  3. Removing the appropriate reminder from the array

I was thinking about an efficient way to run this function and I thought about:

  1. Running this function in an interval of 1 minute that will check for reminders with time due
  2. Trigger a setTimeout which will send the appropriate client his reminder - which means for every reminder there would be set up a setTimeout that runs the function as soon as the time is due.

My question: Is there a better way to implement the solution? From the methods listed, would you say one of the ways is more efficient that the other one?

Many Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If your appointments are in memory, sort them by trigger time. If your appointments are in a database, make sure there's an efficient mechanism for accessing the next appointment to reach its time.

Then, using this efficient sorted access mechanism, find the earliest appointment to fire. If that time has already passed, then fire its notification, remove it from the data structure and repeat. If that time has not yet passed, set a timer for it's scheduled time. When that timer fires remove that event from the data structure and repeat the process by getting the next event to fire and setting a timer for it.

Whenever you add a new appointment, check if it's due time is earlier than the one you have a timer for. If so, release the timer for the one you had and set the timer for the new one.

Whenever an appointment is to be deleted, you first check if it's the next one to notified. If so, stop its timer and configure the next event.

You can likely do all this with four functions that handle the life cycle changes:

addNewEvent(event)        // called when you add a new event to the data
fireEvent(event)          // called when a timer fires to notify of an event
removeEvent(event)        // called when an event is being deleted
configureNextEvent()      // called to configure timer for next event
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda