Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda