Build a Production-Ready SMS Scheduler with NestJS and Plivo - code-examples -

Frequently Asked Questions

Use the `@nestjs/schedule` module with a cron job to trigger the `RemindersService` to check for due reminders and send them via the Plivo API. The cron job's frequency is configurable via the `REMINDER_CHECK_CRON_TIME` environment variable. The service queries the database for pending reminders and updates their status as they're processed.
Plivo is a cloud communications platform that provides the SMS sending functionality. The NestJS application integrates with Plivo's API using the official Plivo Node.js SDK to send SMS messages based on scheduled reminders stored in the database.
TypeORM simplifies database interactions by providing an object-relational mapper (ORM) for TypeScript. It allows you to define entities and interact with the database using familiar JavaScript methods, while PostgreSQL offers a robust and reliable database solution.
The `synchronize: true` setting in TypeORM should *always* be set to `false` in a production environment. While convenient for development, it can lead to data loss. Use TypeORM migrations to manage schema changes safely in production.
Yes, you can potentially adapt the code to use a different database. TypeORM supports various databases. You would need to change the `type` in the `TypeOrmModule` configuration and install the appropriate database driver for TypeORM.
Store your Plivo Auth ID, Auth Token, and sender number in a `.env` file. This file should be added to your `.gitignore` to prevent it from being committed to version control. The `@nestjs/config` module loads these variables into your application's environment.
These libraries provide robust validation and transformation for incoming request data in NestJS. `class-validator` allows you to define validation rules using decorators in your DTOs (Data Transfer Objects), while `class-transformer` automatically transforms request payloads into DTO instances.
The cron schedule is defined by the `REMINDER_CHECK_CRON_TIME` environment variable. Use cron expression syntax to specify the frequency. For example, `* * * * *` represents every minute, and `0 0 * * *` represents every day at midnight. See crontab.guru for help with cron syntax.
If Plivo's API returns an error during SMS sending, the application logs the error, updates the reminder's status to `FAILED`, and stores the error message in the database. An optional `retryFailedReminder` method can be used to attempt resending failed messages.
The article recommends using an LTS (Long-Term Support) version of Node.js for this project. LTS versions receive security updates and bug fixes for an extended period, ensuring stability and compatibility.
Migrations are managed using the TypeORM CLI. After installing it (`npm install -g typeorm`), you can generate migrations with `npm run migration:generate --name=YourMigrationName` and run them with `npm run migration:run`. Ensure you build the project first with `npm run build` to compile the migration files.
The `PROCESSING` status is used to prevent race conditions in a multi-instance deployment, ensuring that a reminder is not picked up and processed multiple times concurrently. The cron job acquires a lock on pending reminders by setting their status to `PROCESSING` before sending the SMS.
NestJS modules promote modularity and organization. Features like Plivo integration, database interaction, and reminder management are separated into distinct modules. This improves maintainability and testability.
The `sendAt` field in the `Reminder` entity uses the `timestamp with time zone` data type in PostgreSQL to store dates with timezone awareness. It is crucial to consistently store dates in UTC within your database to avoid timezone-related issues.
Your Plivo Auth ID and Auth Token can be found in your Plivo console. After logging in, they are displayed on the main dashboard. You'll also need a Plivo phone number to send SMS messages from, which can be purchased or found in the 'Numbers' section under 'Messaging'.