Reusable Node Code Modules, Part 8 - Notifications - SMS
Scalable Alerts: Creating Reusable SMS Notification Modules
SMS notifications are essential for real-time alerts and communication, especially in scenarios where immediate attention is required. Implementing a reusable SMS notification module ensures consistent handling of SMS notifications across projects. This guide covers how to structure reusable SMS notification modules in Node.js using Express, manage notifications efficiently, and integrate common libraries.
Common Libraries and Tools
1. Twilio
Twilio is a popular cloud communications platform that allows you to send and receive SMS messages, make voice calls, and perform other communication functions using its web service APIs
Key Features
Global Reach: Supports sending SMS to almost any country
Robust API: Provides a comprehensive API for SMS, voice, and other communication channels
Scalability: Handles large volumes of messages
Delivery Reports: Provides detailed delivery reports and status callbacks
2. Nexmo (Vonage API)
Nexmo, now part of Vonage, provides APIs for SMS, voice, and phone verifications
Key Features
Global SMS Coverage: Offers SMS delivery to a wide range of countries
Two-Factor Authentication: Provides easy integration for two-factor authentication
Delivery Receipts: Detailed delivery receipts and status callbacks
Virtual Numbers: Offers virtual numbers for receiving SMS
3. Plivo
Plivo is a cloud communications platform that offers APIs for SMS, voice, and messaging
Key Features
Global Reach: Supports sending SMS to over 190 countries
Scalable API: Designed to handle high volumes of messages
Short Codes: Supports short codes for high-volume messaging
Cost-Effective: Competitive pricing for bulk messaging
4. Amazon SNS (Simple Notification Service)
Amazon SNS is a fully managed messaging service that supports sending SMS, email, and push notifications
Key Features
Integration with AWS: Seamlessly integrates with other AWS services
Scalability: Automatically scales to handle large volumes of messages
Cost-Effective: Pay-as-you-go pricing model
Delivery Status: Provides delivery status and logs
Comparison
Twilio: Best for a robust and comprehensive API with global reach and detailed delivery reports
Nexmo (Vonage API): Ideal for global SMS coverage with two-factor authentication and virtual number support
Plivo: Suitable for high-volume messaging with competitive pricing and short code support
Amazon SNS: Best for seamless integration with AWS services and scalable messaging
Examples and Wrapper Class
Example 1: Twilio
Setup:
$ npm install twilio
Configuration:
import twilio from 'twilio';
const accountSid = 'your-account-sid';
const authToken = 'your-auth-token';
const client = twilio(accountSid, authToken);
const sendSMS = async (to, body) => {
try {
const message = await client.messages.create({
to,
from: 'your-twilio-number',
body
});
console.log('Message sent successfully:', message.sid);
} catch (error) {
console.error('Error sending SMS:', error);
}
};
export default sendSMS;
Wrapper Class:
class TwilioClient {
constructor() {
this.client = twilio(accountSid, authToken);
}
async sendSMS(to, body) {
try {
const message = await this.client.messages.create({
to,
from: 'your-twilio-number',
body
});
console.log('Message sent successfully:', message.sid);
} catch (error) {
console.error('Error sending SMS:', error);
}
}
}
export default new TwilioClient();
Example 2: Nexmo (Vonage API)
Setup:
$ npm install @vonage/server-sdk
Configuration:
import Vonage from '@vonage/server-sdk';
const vonage = new Vonage({
apiKey: 'your-api-key',
apiSecret: 'your-api-secret'
});
const sendSMS = async (to, text) => {
vonage.message.sendSms('your-nexmo-number', to, text, (err, responseData) => {
if (err) {
console.error('Error sending SMS:', err);
} else {
if (responseData.messages[0].status === '0') {
console.log('Message sent successfully');
} else {
console.error('Message failed with error:', responseData.messages[0]['error-text']);
}
}
});
};
export default sendSMS;
Wrapper Class:
class NexmoClient {
constructor() {
this.vonage = new Vonage({
apiKey: 'your-api-key',
apiSecret: 'your-api-secret'
});
}
sendSMS(to, text) {
this.vonage.message.sendSms('your-nexmo-number', to, text, (err, responseData) => {
if (err) {
console.error('Error sending SMS:', err);
} else {
if (responseData.messages[0].status === '0') {
console.log('Message sent successfully');
} else {
console.error('Message failed with error:', responseData.messages[0]['error-text']);
}
}
});
}
}
export default new NexmoClient();
Example 3: Plivo
Setup:
$ npm install plivo
Configuration:
import plivo from 'plivo';
const client = new plivo.Client('your-auth-id', 'your-auth-token');
const sendSMS = async (to, text) => {
try {
const response = await client.messages.create(
'your-plivo-number',
to,
text
);
console.log('Message sent successfully:', response);
} catch (error) {
console.error('Error sending SMS:', error);
}
};
export default sendSMS;
Wrapper Class:
class PlivoClient {
constructor() {
this.client = new plivo.Client('your-auth-id', 'your-auth-token');
}
async sendSMS(to, text) {
try {
const response = await this.client.messages.create(
'your-plivo-number',
to,
text
);
console.log('Message sent successfully:', response);
} catch (error) {
console.error('Error sending SMS:', error);
}
}
}
export default new PlivoClient();
Example 4: Amazon SNS
Setup:
$ npm install aws-sdk
Configuration:
import AWS from 'aws-sdk';
AWS.config.update({
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key',
region: 'your-region'
});
const sns = new AWS.SNS();
const sendSMS = async (phoneNumber, message) => {
const params = {
Message: message,
PhoneNumber: phoneNumber
};
try {
const data = await sns.publish(params).promise();
console.log('Message sent successfully:', data);
} catch (error) {
console.error('Error sending SMS:', error);
}
};
export default sendSMS;
Wrapper Class:
class SNSClient {
constructor() {
AWS.config.update({
accessKeyId: 'your-access-key-id',
secretAccessKey: 'your-secret-access-key',
region: 'your-region'
});
this.sns = new AWS.SNS();
}
async sendSMS(phoneNumber, message) {
const params = {
Message: message,
PhoneNumber: phoneNumber
};
try {
const data = await this.sns.publish(params).promise();
console.log('Message sent successfully:', data);
} catch (error) {
console.error('Error sending SMS:', error);
}
}
}
export default new SNSClient();