DEV Community

Cover image for Using Amazon SQS in Nest.
Evan Hameed
Evan Hameed

Posted on

Using Amazon SQS in Nest.

I have been working lately with Amazon SQS on a nest project. I thought of sharing the experience with others because it was a headache for me to actually set it up the right way in NestJS.

After trying some different approaches, here is my ultimate SQS setup in nestJS.

Github: https://github.com/evanhameed99/amazon-sqs-nest

First of all: Install below dependencies :

npm i @ssut/nestjs-sqs
npm i aws-sdk
Enter fullscreen mode Exit fullscreen mode

I will mainly tackle two different scenarios:

  • Producing messages to an existing SQS.
  • Consuming messages from an existing SQS.
  1. Setting Up the Producer:
  • Create producer.module.ts
import { Module } from '@nestjs/common';
import { SqsModule } from '@ssut/nestjs-sqs';
import { MessageProducer } from './producer.service';
import * as AWS from 'aws-sdk';
import { config } from '../config';

AWS.config.update({
    region: config.AWS_REGION, // aws region
    accessKeyId: config.ACCESS_KEY_ID, // aws access key id
    secretAccessKey: config.SECRET_ACCESS_KEY, // aws secret access key
});


@Module({
    imports: [
        SqsModule.register({
            consumers: [],
            producers: [
                {
                    name: config.TEST_QUEUE, // name of the queue
                    queueUrl: config.TEST_QUEUE_URL, 
                    region: config.AWS_REGION, // url of the queue
                },
            ],
        }),
    ],
    controllers: [],
    providers: [MessageProducer],
    exports: [MessageProducer]
})
export class ProducerModule { }

Enter fullscreen mode Exit fullscreen mode
  • Create Producer function in producer.service.ts
import { Injectable } from '@nestjs/common';
import { SqsService } from '@ssut/nestjs-sqs';
import { config } from '../config';
@Injectable()
export class MessageProducer {
    constructor(private readonly sqsService: SqsService) { }
    async sendMessage(body: any) {

        const message: any = JSON.stringify(body);

        try {
            await this.sqsService.send(config.TEST_QUEUE, message);
        } catch (error) {
            console.log('error in producing image!', error);
        }

    }
}

Enter fullscreen mode Exit fullscreen mode
  • Lastly Import the producer in the root app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ProducerModule } from './producer/producer.module';

@Module({
  imports: [
    ProducerModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}


Enter fullscreen mode Exit fullscreen mode
  1. Setting up the consumer.
  • Create consumer.module.ts
import { Module } from '@nestjs/common';
import { SqsModule } from '@ssut/nestjs-sqs';
import { MessageHandler } from './messageHandler';
import * as AWS from 'aws-sdk';
import { config} from '../config';


AWS.config.update({
    region: config.AWS_REGION,
    accessKeyId: config.ACCESS_KEY_ID,
    secretAccessKey: config.SECRET_ACCESS_KEY,
});
@Module({
    imports: [
        SqsModule.register({
            consumers: [
                {
                    name: config.TEST_QUEUE, // name of the queue 
                    queueUrl: config.TEST_QUEUE, // the url of the queue
                    region: config.AWS_REGION,
                },
            ],
            producers: [],
        }),
    ],
    controllers: [],
    providers: [MessageHandler],
})
export class ConsumerModule { }
Enter fullscreen mode Exit fullscreen mode
  • Set up your message handler to consume the messages messageHandler.ts
import { Injectable } from '@nestjs/common';
import { SqsMessageHandler } from '@ssut/nestjs-sqs';
import * as AWS from 'aws-sdk';
import { config } from '../config';


console.log('config.AWS_REGION', config);
@Injectable()
export class MessageHandler {
    constructor() { }
    @SqsMessageHandler(config.TEST_QUEUE, false)
    async handleMessage(message: AWS.SQS.Message) {
        const obj: any = JSON.parse(message.Body) as {
            message: string;
            date: string;
        };
        const { data } = JSON.parse(obj.Message);

        // use the data and consume it the way you want // 

    }
}

Enter fullscreen mode Exit fullscreen mode
  • Lastly import the consumer module in app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConsumerModule } from './consumer/consumer.module';
import { ProducerModule } from './producer/producer.module';

@Module({
  imports: [
    ProducerModule,
    ConsumerModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}


Enter fullscreen mode Exit fullscreen mode

Link to my template repo.

Latest comments (4)

Collapse
 
manjunathj profile image
Manjunath

Does SQS consumer run continuously i.e. Will consumer run forever? Or only once?

Collapse
 
hungpv1995 profile image
hungpv1995

Great!
I have followed your instructions. But sometimes, I have problem as shown below. I don't know what is the cause of this problem and how to fix it. Help me please
SQSError: SQS receive message failed: read ECONNRESET
at toSQSError (/app/node_modules/sqs-consumer/dist/consumer.js:45:22)
at Consumer.receiveMessage (/app/node_modules/sqs-consumer/dist/consumer.js:157:19)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Consumer instance at:

Image description

Collapse
 
daruns profile image
Darun Salam

Well done Evan! very well explained and documented.
NestJs is growing a bit fast recently

Collapse
 
evanhameed99 profile image
Evan Hameed

Thank you Darun! I hope it helped.