Skip to main content

Delayed messages - How to delay message processing?

· 4 min read
Ümit Çakmak

We are excited to announce the release of Delayed Messages functionality in message queues. Delayed messages are a special type of message that are submitted to a message queue and processed after the specified delay duration instead immediately.

Basics - What is a message queue?

Message queues are primarily used to process activities in parallel to service execution. As an example, for a sign-up service, you might want to send a welcome email to the new user at the end of the service. However, sending an email might take time, and you do not wish to delay the sign-up service but return an immediate response to the user. In this case, you can define a queue to send the welcome email and submit a message to this queue from the sign-up service. The submitted message will be handled by the service assigned to the queue and processed asynchronously.

Message Queue

Message queues allow different parts of your application to communicate and perform activities asynchronously. A message queue provides a buffer that temporarily stores messages and dispatches them to their consuming service. The messages are usually small, and can be things like requests, replies or error messages, etc. To send a message, an endpoint service or a task service called a producer needs to have a Submit Message to Queue node that sends a message to the queue when ran or the submitMessage method of client library needs to be called. The message is stored in the queue until the target service called a consumer retrieves the message and performs activities with it. It is also possible to delay the messsages submitted to the queue so that these messsages can be processed at a later time.

What are delayed messages?

Usually, when a message is submitted to a queue, it is immediately picked up by an available consumer and processed. Whereas for delayed messages, as its name implies, the message queue applies a delay duration from the submission of the message until its actual processing.

There are some scenarios in real life where messages must be sent with a delay, such as a new user onboarding process where you send a welcome email after 12 hours of sign up and a follow up informative email after 3 days later to keep your customers engaged or for an e-commerce application you can send a reminder message to a customer with products in a shopping cart not checked out after 24 hours.

How to use delayed messages in Altogic?

To use delayed mesages, first you need to have a mesage queue and a mapped consumer service to process submitted messages. You can get more information on creating message queues from the product guide documentation.

Currently there are two ways to submit delayed messages to a queue. If you are using Altogic designer to build your cloud functions you can use the Submit Message to Queue node and specify the message delay and unit of delay parameters. If you are using the Altogic Client Library, you can use the submitMessage method of QueueManager to specify the delay duration when submitting your messages to a queue.

Submitting delayed messages through Altogic Designer

Submit message to a queue

Submitting delayed messages through client API

import { createClient } from "altogic";

let envUrl = process.env.REACT_APP_ALTOGIC_ENV_URL;
let clientKey = process.env.REACT_APP_ALTOGIC_CLIENT_KEY;

// Create Altogic client instance
const altogic = createClient(envUrl, clientKey);

let queueName = "sendWelcomeEmail";
let messageBody = {
name: "Rooby",
email: "[email protected]",
};

// Process the message after 24 hours (e.g., 60 x 60 x 24 = 86400 seconds)
const { info, errors } = await altogic.queue.submitMessage(queueName, messageBody, 86400);

If you have any questions about delayed messages or want to share what you have built, please post a message in our community forum or discord channel.