Home Projects Portfolio Dashboard Export PDF Log in

Building Robust WhatsApp Notifications with QStash and Serverless Queues

The Challenge

In the Cronix project, ensuring timely and reliable communication with users and owners is paramount. We recently tackled the challenge of delivering critical notifications, particularly via WhatsApp, in a highly dependable manner. Directly sending WhatsApp messages can encounter transient network issues, API rate limits, or service outages. For crucial owner notifications, we needed a system that guaranteed delivery, even if the primary service was temporarily unavailable or overwhelmed.

The Approach: Adopting Serverless Queuing

To overcome these challenges and ensure message delivery guarantees, we decided to implement a serverless message queuing system. This approach decouples the message sending request from the actual delivery mechanism, allowing for retries, error handling, and eventual consistency.

Phase 1: Implementing WhatsApp Message Queuing with QStash

Our primary solution involved integrating QStash, a serverless messaging queue from Upstash. QStash provides a robust, HTTP-based queuing mechanism with built-in features like automatic retries and dead-letter queueing, perfectly suited for our needs. When a WhatsApp message needs to be sent, our application now publishes it to QStash, which then reliably delivers it to our designated handler.

import { Client } from "@upstash/qstash";

// Initialize QStash client with your token
const qstash = new Client({ token: process.env.QSTASH_TOKEN });

interface WhatsAppMessagePayload {
  recipientId: string;
  messageContent: string;
}

async function enqueueWhatsAppNotification(payload: WhatsAppMessagePayload) {
  try {
    await qstash.publish({
      url: "https://api.example.com/webhooks/whatsapp-handler", // Our backend endpoint to process messages
      body: JSON.stringify(payload),
      delay: 0, // Messages are typically sent immediately, but can be delayed
      retries: 5, // QStash will retry delivery up to 5 times
    });
    console.log("WhatsApp message successfully enqueued.");
  } catch (error) {
    console.error("Failed to enqueue WhatsApp message:", error);
    // Log or alert for immediate failures to enqueue
  }
}

// Example usage:
enqueueWhatsAppNotification({
  recipientId: "whatsapp:+15551234567",
  messageContent: "Your Cronix task has been completed!"
});

This TypeScript snippet demonstrates how an application publishes a message to QStash. QStash then takes responsibility for delivering this payload to our whatsapp-handler endpoint, ensuring that even if the handler is temporarily offline, the message will eventually be processed thanks to QStash's retry mechanisms.

Phase 2: Updating Owner Notifications for Reliability

With the QStash-powered queuing system in place, we extended its use to critical owner notifications. This means that important alerts for Cronix owners, which were previously susceptible to immediate delivery failures, now benefit from the same robust, retry-enabled delivery pipeline. Owners receive their critical updates consistently, improving operational awareness.

Phase 3: Enhancing PWA Dark Mode Aesthetics

While not directly related to message queuing, this commit also included significant improvements to the Progressive Web App (PWA)'s dark mode aesthetics. Enhancements focused on visual consistency and user comfort, ensuring that the application remains pleasant to use in low-light environments. This contributes to a better overall user experience, supporting Cronix's commitment to accessibility and modern design.

Final Numbers

Feature Before After
WhatsApp Reliability Potential for dropped messages Guaranteed eventual delivery (QStash retries)
Owner Notification Immediate failure possible Reliable, retried delivery
PWA Dark Mode Inconsistent/less refined Enhanced aesthetics, user comfort

Key Insights

Implementing serverless message queues like QStash drastically improves the reliability of critical communications, especially for services like WhatsApp that can face external API constraints. Decoupling the sending logic from delivery ensures messages eventually reach their destination, enhancing both user and owner experience. Additionally, continuous attention to UI details, such as dark mode aesthetics, reinforces the commitment to a polished and user-friendly application.


Generated with Gitvlg.com

Building Robust WhatsApp Notifications with QStash and Serverless Queues
Luis Romero

Luis Romero

Author

Share: