DEV Community

Shanu
Shanu

Posted on

Implementing Push Notifications Using JavaScript: A Production-Grade Approach

In this post, you will learn how to implement push notifications using JavaScript by following production-grade best practices. One of the best things is that I will provide a folder structure too, so that you can set up your project easily.

Setting up push notifications in a real-world app needs careful planning. I'll show you how to build this feature in a professional Node.js app. We'll cover important parts like how to organize your code, keep things secure, and make sure it works well even as your app grows.

To get started, you need a library to help you send push notifications from your Node.js server. The web-push library provides tools for sending notifications and managing the necessary keys.

1. Push Notification: Project Structure

First, let’s set up the project structure to maintain a clean and scalable codebase:

/notification-service
├── /config
│   ├── default.js
│   └── production.js
├── /controllers
│   └── notificationController.js
├── /models
│   └── user.js
├── /routes
│   └── notificationRoutes.js
├── /services
│   ├── notificationService.js
│   ├── subscriptionService.js
│   └── webPushService.js
├── /utils
│   └── errorHandler.js
├── /tests
│   └── notification.test.js
├── app.js
├── package.json
├── .env
└── README.md
Enter fullscreen mode Exit fullscreen mode

Required NPM Packages

Before diving into the implementation, ensure you have the following NPM packages installed:

  • express: A minimal and flexible Node.js web application framework.
  • mongoose: An ODM (Object Data Modeling) library for MongoDB and Node.js.
  • web-push: A library for sending push notifications using the Web Push Protocol.
  • dotenv: A zero-dependency module that loads environment variables from a .env file.
  • supertest: A library for testing HTTP assertions in Node.js.

Install these packages using npm:


bash

npm install express mongoose web-push dotenv supertest
Enter fullscreen mode Exit fullscreen mode

2. Push Notification: Project Configuration

Create configuration files for different environments (e.g., development, production). These files store environment-specific settings.

// /config/default.js
module.exports = {
    server: {
        port: 3000,
        env: 'development'
    },
    pushNotifications: {
        publicVapidKey: process.env.VAPID_PUBLIC_KEY,
        privateVapidKey: process.env.VAPID_PRIVATE_KEY,
        gcmApiKey: process.env.GCM_API_KEY
    },
    db: {
        uri: process.env.MONGO_URI
    }
};
Enter fullscreen mode Exit fullscreen mode
// /config/production.js
module.exports = {
    server: {
        port: process.env.PORT || 3000,
        env: 'production'
    },
    // Same structure as default, with production-specific values
};
Enter fullscreen mode Exit fullscreen mode

3. Modeling the Database

Use Mongoose to define your user schema and notification subscriptions.

// /models/user.js
const mongoose = require('mongoose');

const subscriptionSchema = new mongoose.Schema({
    endpoint: String,
    keys: {
        p256dh: String,
        auth: String
    }
});

const userSchema = new mongoose.Schema({
    email: { type: String, required: true, unique: true },
    subscriptions: [subscriptionSchema],
    preferences: {
        pushNotifications: { type: Boolean, default: true }
    }
});

module.exports = mongoose.model('User', userSchema);
Enter fullscreen mode Exit fullscreen mode

4. Notification Services

Modularize the logic for handling notifications into services.

// /services/webPushService.js
const webPush = require('web-push');
const config = require('config');

webPush.setVapidDetails(
    'mailto:example@yourdomain.org',
    config.get('pushNotifications.publicVapidKey'),
    config.get('pushNotifications.privateVapidKey')
);

module.exports = {
    sendNotification: async (subscription, payload) => {
        try {
            await webPush.sendNotification(subscription, JSON.stringify(payload));
        } catch (error) {
            console.error('Error sending notification', error);
        }
    }
};
Enter fullscreen mode Exit fullscreen mode
// /services/notificationService.js
const User = require('../models/user');
const webPushService = require('./webPushService');

module.exports = {
    sendPushNotifications: async (userId, payload) => {
        const user = await User.findById(userId);
        if (user && user.preferences.pushNotifications) {
            user.subscriptions.forEach(subscription => {
                webPushService.sendNotification(subscription, payload);
            });
        }
    }
};
Enter fullscreen mode Exit fullscreen mode

5. Controller Logic

Handle API routes and integrate services.

// /controllers/notificationController.js
const notificationService = require('../services/notificationService');

exports.sendNotification = async (req, res, next) => {
    try {
        const { userId, title, body } = req.body;
        const payload = { title, body };
        await notificationService.sendPushNotifications(userId, payload);
        res.status(200).json({ message: 'Notification sent successfully' });
    } catch (error) {
        next(error);
    }
};
Enter fullscreen mode Exit fullscreen mode

6. Routing

Set up routes for your API.

// /routes/notificationRoutes.js
const express = require('express');
const router = express.Router();
const notificationController = require('../controllers/notificationController');

router.post('/send', notificationController.sendNotification);

module.exports = router;
Enter fullscreen mode Exit fullscreen mode

7. Error Handling

Centralize error handling to ensure the app doesn’t crash.

// /utils/errorHandler.js
module.exports = (err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send({ error: 'Something went wrong!' });
};
Enter fullscreen mode Exit fullscreen mode

8. Application Entry Point

Initialize the application and connect to the database.

// app.js
const express = require('express');
const mongoose = require('mongoose');
const config = require('config');
const notificationRoutes = require('./routes/notificationRoutes');
const errorHandler = require('./utils/errorHandler');

const app = express();

app.use(express.json());
app.use('/api/notifications', notificationRoutes);
app.use(errorHandler);

mongoose.connect(config.get('db.uri'), {
    useNewUrlParser: true,
    useUnifiedTopology: true
})
    .then(() => console.log('MongoDB connected...'))
    .catch(err => console.error('MongoDB connection error:', err));

const PORT = config.get('server.port');
app.listen(PORT, () => console.log(`Server running in ${config.get('server.env')} mode on port ${PORT}`));
Enter fullscreen mode Exit fullscreen mode

9. Security Practices

  • Environment Variables: Store sensitive information like API keys and database URIs in environment variables.
  • HTTPS: Serve your application over HTTPS to secure communication between clients and the server.
  • Content Security Policy (CSP): Implement CSP headers to prevent cross-site scripting (XSS) attacks.
  • Rate Limiting: Use middleware like express-rate-limit to protect your API from brute-force attacks.

10. Testing

Write tests to ensure your service works as expected under various conditions.

// /tests/notification.test.js
const request = require('supertest');
const app = require('../app');

describe('Notification API', () => {
    it('should send a notification', async () => {
        const res = await request(app)
            .post('/api/notifications/send')
            .send({ userId: 'someUserId', title: 'Test', body: 'This is a test' });
        expect(res.statusCode).toEqual(200);
        expect(res.body.message).toBe('Notification sent successfully');
    });
});
Enter fullscreen mode Exit fullscreen mode

11. Deploying to Production

  • CI/CD Pipeline: Set up a CI/CD pipeline using tools like Jenkins, GitHub Actions, or GitLab CI to automate testing, building, and deploying your application.
  • Containerization: Dockerize your application to ensure consistency across different environments.
  • Monitoring: Use monitoring tools like Prometheus and Grafana to track the health and performance of your application.

12. Scaling

  • Horizontal Scaling: Deploy multiple instances of your service behind a load balancer to handle high traffic.
  • Database Scaling: Implement sharding or replica sets in MongoDB for horizontal scaling of your database.

This production-grade setup ensures that your push notification system is scalable, secure, and maintainable. The code is organized to support easy testing, deployment, and monitoring, following industry best practices. If you have any further questions or need specific implementation details, feel free to ask!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.