Giter VIP home page Giter VIP logo

symfony-messenger-sqs's Introduction

This package is deprecated in favor of the more generic bref/symfony-messenger.


Bridge to use Symfony Messenger with SQS on AWS Lambda with Bref.

Installation

This guide assumes that:

  • Symfony is installed
  • Symfony Messenger is installed
  • Bref is installed and configured to deploy Symfony
  • a SQS queue has already been created

First, install this package:

composer require bref/symfony-messenger-sqs

Next, register the bundle in config/bundles.php:

return [
    ...
    Bref\Messenger\BrefMessengerBundle::class => ['all' => true],
];

Next, configure Symfony Messenger to dispatch a message via SQS:

# config/packages/messenger.yaml

framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
             'App\Message\MyMessage': async

Here, the MyMessage class will be dispatch to the async transport. We can now configure the async transport to use our SQS queue.

To do that, let's configure the MESSENGER_TRANSPORT_DSN environment variable to contain the URL of the queue:

MESSENGER_TRANSPORT_DSN=https://sqs.us-east-1.amazonaws.com/123456789101/my-queue

Sending messages

Now that Messenger is configured with SQS, we can send messages using the MessageBusInterface. For example, in a controller:

class DefaultController extends AbstractController
{
    public function index()
    {
        $this->dispatchMessage(new App\Message\MyMessage());
    }
}

Read the Symfony documentation to learn more.

Processing message

Messages are sent to SQS, we now need to process those messages asynchronously.

We can create a Lambda to do that in serverless.yml:

functions:
    worker:
        handler: consumer.php
        timeout: 120 # in seconds
        reservedConcurrency: 5 # max. 5 messages processed in parallel
        layers:
            - ${bref:layer.php-73}
        events:
            -   sqs:
                    arn: arn:aws:sqs:us-east-1:123456789101:my-queue
                    # Only 1 item at a time to simplify error handling
                    batchSize: 1

The Lambda handler will be consumer.php, a file we must create:

<?php declare(strict_types=1);

use Bref\Messenger\Sqs\SqsConsumer;

require __DIR__ . '/config/bootstrap.php';

lambda(function ($event) {
    $kernel = new \App\Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
    $kernel->boot();

    $sqsConsumer = $kernel->getContainer()->get(SqsConsumer::class);
    $sqsConsumer->consumeLambdaEvent($event);
});

Finally, we must configure the SqsConsumer service in config/services.yaml (this configuration relies on autowiring being enabled by default):

services:
    ...

    Bref\Messenger\Sqs\SqsConsumer:
        arguments:
            # Inject the transport name used in config/packages/messenger.yaml 
            $transportName: 'async'
        public: true

symfony-messenger-sqs's People

Contributors

mnapoli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

symfony-messenger-sqs's Issues

The service "Bref\Messenger\Sqs\SqsTransportFactory" has a dependency on a non-existent service "Aws\Sqs\SqsClient".

After following the instructions, I ended up with an error about non-existent service.

The service "Bref\Messenger\Sqs\SqsTransportFactory" has a dependency on a non-existent service "Aws\Sqs\SqsClient".

I've realised that I needed to configure it myself and it might help others if this was included in the readme.

Aws\Sqs\SqsClient:
     arguments:
          - { version: latest, region: eu-west-2, credentials: { key: xxx, secret: xxx } }

I believe that credentials is optional, if you have credentials stores in ~/.aws/credentials

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.