Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Message Queue
    • Quick start
    • Supported tools
      • Python
      • Node.js
      • PHP
      • Celery
      • JMS
      • Laravel
      • Symfony
      • Terraform
      • Golang
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Public materials
  • FAQ

In this article:

  • Installing
  • Getting started
  • Example
  1. Getting started
  2. Code snippets
  3. Node.js

Example of using Yandex Message Queue on Node.js

Written by
Yandex Cloud
Improved by
Alex
Updated at March 28, 2025
  • Installing
  • Getting started
  • Example

Node.js is a runtime environment for JavaScript applications. With the AWS SDK for JavaScript, you can manage Message Queue message queues and send and receive messages in Node.js.

InstallingInstalling

Install the AWS SDK for JavaScript in Node.js by following the instructions on the official site.

npm install @aws-sdk/client-sqs@3.445.0

Getting startedGetting started

  1. Create a service account.
  2. Assign the editor role to the service account.
  3. Create a static access key.

Set the environment variables:

export AWS_ACCESS_KEY_ID="<access_key_ID>"
export AWS_SECRET_ACCESS_KEY="<secret_key>"

ExampleExample

In this example:

  1. A connection with Message Queue is established.
  2. A message queue named mq_example_nodejs_sdk is created.
  3. A message with the text test message is sent to the queue.
  4. The message is read from the queue and displayed in the terminal.
  5. The message queue is deleted.
var AWS = require('aws-sdk');

var mq = new AWS.SQS({
    'region': 'ru-central1',
    'endpoint': 'https://message-queue.api.cloud.yandex.net',
});

async function createQueue() {
    params = {
        'QueueName': 'mq_example_nodejs_sdk',
    }

    result = await mq.createQueue(params).promise();
    queueUrl = result['QueueUrl'];

    console.log('Queue created, URL: ' + queueUrl);

    return queueUrl;
}

async function sendMessage(queueUrl) {
    params = {
        'QueueUrl': queueUrl,
        'MessageBody': 'test message',
    }

    result = await mq.sendMessage(params).promise();

    console.log('Message sent, ID: ' + result['MessageId']);
}

async function receiveMessage() {
    params = {
        'QueueUrl': queueUrl,
        'WaitTimeSeconds': 10,
    }

    result = await mq.receiveMessage(params).promise();

    result['Messages'].forEach(async function(msg) {
        console.log('Message received')
        console.log('ID: ' + msg['MessageId'])
        console.log('Body: ' + msg['Body'])

        deleteParams = {
            'QueueUrl': queueUrl,
            'ReceiptHandle': msg['ReceiptHandle'],
        }

        await mq.deleteMessage(deleteParams).promise()
    })
}

async function deleteQueue() {
    params = {
        'QueueUrl': queueUrl,
    }

    result = await mq.deleteQueue(params).promise();

    console.log('Queue deleted')
}

async function main() {
    queueUrl = await createQueue();
    await sendMessage(queueUrl);
    await receiveMessage(queueUrl);
    await deleteQueue(queueUrl);
}

main()

Was the article helpful?

Previous
Python
Next
PHP
© 2025 Direct Cursus Technology L.L.C.