Please excuse our look. We're just getting started here.

Want to learn more about Twilio Forums? Check out our FAQ page here.

I tend to overthink things and I just want some advice. - NodeJS Advice Needed

The project I'm currently working on requires Twilio for follow-up text post-lead creation, then a follow-up on a daily basis for approximately 5 days. As I was building it out in nodejs it dawned on me that:

1) This will become a problem when I go to update the nodejs server and reboot losing all the records that are stored in memory.

2) This will become a bigger issue when it comes to scaling because all those processes are going to be running at the same time.

The project is simple enough but, I tend to overthink things and I'm hoping to get some guidance on if i'm overthinking this or if there is a better way to do this.

Current direction: building out Twilio API calls/Routes via nodejs

Possible other direction: building out the calls via Twilio Functions, is that even possible for this?

Best Answer

Answers

  • @pnash, thanks for the response! I'm currently using node scheduler and toad scheduler modules to control the difference between interval texts, eg. following up 30 minutes after the initial call with a follow-up phone call. I figured I should use a database to store the numbers that were sent a text and haven't opted out of receiving text notifications, thanks for the advice. :)

    Last question, is it possible to do all this via Twilio functions? I'm only going the nodejs route because that's what I'm used to writing in or is that something that will have an issue scaling, what's the downside to going that route?

  • I am honestly surprised by how popular node-scheduler is given it isn't resilient to restarts. node-scheduler does suggest Agenda as a persistent alternative, it uses MongoDB as the database rather than BullMQ's use of Redis, depending on what you are more comfortable with.

    You probably should store numbers that have opted out of receiving messages too! And if someone opts out in the middle of your follow ups, cancel the jobs in the future for them too!

    Twilio Functions has no scheduling capability, so while you could run a Function to send a message or queue a job to send a message at another time, you couldn't do that triggering from Functions. So, you would need a separate system that would trigger the jobs.

  • @pnash I appreciate the follow up, I actually decided on a direction already, let me know what you think of this and again I appreciate your feedback and time.

    1) I'm going to use BullMQ to set up the initial text messages for the first day, that's because if I have an emergency shutdown or what-not I'd rather have those jobs ready to go.

    2) I'm going to be using the new Message Scheduling to control the 5 day follow-up text messages.

    3) I'll be storying the STOP numbers either via SQL or MongoDB, I'm unsure yet it's been awhile since I've had to use SQL but, thanks again for all your advice if you have any feedback on this direction, let me know. :)

  • Great! Let me know how it all goes!

  • @pnash Oh and to answer your question about the node scheduler being used alot, it's because atleast for me when I type in "node scheduler" into google, that one is king. This is my first time having to deal with any type of scheduler and it made sense for me to use something that similiar to settimeouts or vanilla js type functions, but I guess that's why you live and learn and hopefully if someone in the future comes across this conversation they don't make that same mistake, haha.

  • That makes sense! And I'm not saying it's not useful to schedule things in memory, just seems like something you'll eventually end up changing once you realise jobs keep getting lost with restarts! Hopefully you will find a full queueing system is a lot more robust and gives you greater insight into the number of jobs you are scheduling how they are performing.

  • lewis.adam1992
    edited January 24

    Hey buddy @pnash , sorry to bug you again, but the Twilio scheduled messaging service doesn't seem to work for me. All the scheduled text messages are returning accepted and then sending everything at once.

    UTC String being sent to sendAt



    I'm unsure of exactly what i'm missing, is there some sort of setting I should be turning on maybe? Idk, but I even hardcoded UTC time to a specific day, two days from now and I'm receiving the same problem.

  • Are you sending a ScheduleType parameter set to "fixed" too? What is the code you're using to send the message?

  • lewis.adam1992
    edited January 25

    Okay, so I actually discovered the solution for this, there's two things going on.

    1. If you use POSTman to send the API request your string for the date has to formatted as such: 2022-01-25T02:21:00Z
      1. But the example in the documentation shows up like this: Tue Nov 30 2021 12:36:27 GMT-0800 (Pacific Standard Time)
        1. More specifically: 2021-11-30T20:36:27.000Z
          1. Note the .000Z at the end of the time stamp
        2. This wouldn't be an issue but, when I run a similar date (obviously using the parameters of the beta I have to change the time frame but keep the same format) I get this:
          1.  "message": "Invalid SendAt: 2022-01-25T20:36:27:000Z. SendAt must be a valid ISO 8601 formatted datetime string: yyyy-MM-dd'T'HH:mm:ss'Z'",
          2. Notice the time stamp is accurate, but then when I would remove the three zero's prepended to the Z it worked just fine, now that's in POSTman
    2. If you use NodeJS, you have to write the code as such: sendDate = new Date(Date.UTC(currentYear, currentMonth, currentDay, timeToSend, currentMinute, 0)).toISOString().replace(".000", "");
      1. This seems to have solved the problem, the last problem I ran into was I discovered I was one minor version from getting the message scheduling code, so I had to update everything seems to be on the up and up now, but yeah i just wanted to be thorough as possible in my explanation for the next dev that might have this issue. 😀


If this is an emergency, please contact Twilio Support. This is not an official Support channel. https://support.twilio.com/
Have an urgent question?
Please contact Twilio Support. This is not an official Support channel.
Contact Support