The TTL isn’t being set on messages added to the queue using this code, and I can’t fathom why that might be, even after trying multiple different iterations. Stack Overflow wants me to add more details, but it’s a pretty succinct problem.
// Function to send a message to the queue with a TTL
const sendToQueue = async (messageContent, ttlInSeconds) => {
const credential = new DefaultAzureCredential();
const queueServiceClient = new QueueServiceClient(
`https://${storageAccountName}.queue.core.windows.net`,
credential
);
const queueClient = queueServiceClient.getQueueClient(queueName);
const message = JSON.stringify({ message: messageContent });
// Set the time-to-live (TTL) for the message (in seconds)
const options = {
timeToLive: ttlInSeconds // TTL is in seconds
};
await queueClient.sendMessage(message, options);
};
app.http('alert', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request, context) => {
console.log("JavaScript HTTP trigger function processed a request.");
const messageContent = request.query.get("message"); // Get the 'message' parameter from URL
if (messageContent) {
try {
// Your message sending logic here
const ttlInSeconds = 3600; // Set TTL to 1 hour (adjust as needed)
const result = await sendToQueue(messageContent, ttlInSeconds); // Pass TTL in seconds
return {
status: 200,
body: "Message added to the queue successfully.",
};
} catch (error) {
console.log("Error adding message to the queue:", error);
return {
status: 500,
body: "Internal server error.",
};
}
} else {
return {
status: 400,
body: "Please pass a 'message' parameter in the URL.",
};
}
}
});
2
Answers
Got it. :)
I believe you are not able to set the message TTL is because you are using incorrect property name. The correct property name is
messageTimeToLive
based on the documentation available here: https://learn.microsoft.com/en-us/javascript/api/%40azure/storage-queue/queuesendmessageoptions?view=azure-node-latest#@azure-storage-queue-queuesendmessageoptions-messagetimetolive.Please try with the following code: