Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!