Frequently Asked Questions
You can schedule SMS messages by sending a POST request to the /schedule endpoint of your NestJS application. This endpoint accepts a JSON payload with the recipient's phone number, the message content, and the desired send time. The message will then be stored in the database and sent at the specified time via the Vonage Messages API.
The Vonage Messages API is a versatile API that enables sending and receiving messages through various channels, including SMS. In this NestJS application, it's the core component responsible for delivering the scheduled SMS messages to the recipients' phones.
PostgreSQL is used as the database for this SMS scheduling application because it's a robust and reliable open-source relational database. It's well-suited for persisting scheduled jobs and ensuring that message data is stored securely and efficiently.
You should never set synchronize: true in a production TypeORM configuration. While convenient for development, it can lead to data loss in production. Always use migrations to manage schema changes safely and predictably.
Yes, you can use Prisma as an alternative ORM for this project. The article mentions Prisma as a viable option, though the provided code examples demonstrate the setup with TypeORM.
Obtain your API key and secret from the Vonage API Dashboard. Create a Vonage application, download the private key, enable the Messages capability, and obtain the Application ID. Purchase a Vonage phone number, link it to your application, and set the Messages API as the default for sending SMS. Don't forget to configure the 10DLC for US numbers in production.
The @nestjs/schedule module provides a declarative way to schedule tasks (cron jobs, timeouts, intervals) within a NestJS application. This project uses it to periodically check for and send SMS messages that are due.
The application tracks message status using an enum with states like PENDING, SENT, FAILED, and optionally PROCESSING. This allows monitoring the lifecycle of each scheduled message and implementing features like retries or error handling.
The VonageService in the application uses the @vonage/server-sdk to send SMS messages. The sendSms method constructs a request object with recipient, sender, and message details. The messages.send method of the SDK then handles the actual sending process via the Vonage Messages API. This method returns a message ID or an error. You can use this ID to later query the message status via the Vonage API or Dashboard.
First, install the NestJS CLI and create a new project using nest new. Install required dependencies like @nestjs/config, @nestjs/schedule, typeorm, @vonage/server-sdk. Set up environment variables in a .env file. Configure the TypeORM connection and generate migrations to create database tables. Initialize the Vonage and scheduling modules and services.
NestJS provides the @nestjs/config module to manage environment variables. You can create a .env file to store configuration values and load them into the ConfigService. Never commit this file to source control.
class-validator and class-transformer are used for robust request data validation. They ensure incoming data conforms to the expected format and data types, preventing common security and data integrity issues.
You'll need Node.js (LTS recommended), npm or yarn, a Vonage API account with a purchased number, access to a PostgreSQL database, basic command-line familiarity, and optionally Docker for containerization.
The article provides an example of a locking mechanism using a PROCESSING status. When a message is picked up by the scheduler, its status is updated to PROCESSING before sending. If another scheduler instance attempts to process the same message, it will detect the changed status and skip the message.