PHPackages                             the-universal-group/laravel-echo-api-gateway - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. the-universal-group/laravel-echo-api-gateway

ActiveLibrary[HTTP &amp; Networking](/categories/http)

the-universal-group/laravel-echo-api-gateway
============================================

Use Laravel Echo with API Gateway Websockets

1(2mo ago)02MITPHPPHP ^8.0|^8.1|^8.2

Since Feb 27Pushed 2mo agoCompare

[ Source](https://github.com/The-Universal-Group/patch-laravel-echo-api-gateway)[ Packagist](https://packagist.org/packages/the-universal-group/laravel-echo-api-gateway)[ Docs](https://github.com/georgeboot/laravel-echo-api-gateway)[ GitHub Sponsors](https://github.com/georgeboot)[ RSS](/packages/the-universal-group-laravel-echo-api-gateway/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (9)Versions (2)Used By (0)

laravel-echo-api-gateway
========================

[](#laravel-echo-api-gateway)

[![CI](https://github.com/georgeboot/laravel-echo-api-gateway/workflows/CI/badge.svg?event=push)](https://github.com/georgeboot/laravel-echo-api-gateway/actions?query=workflow%3ACI)[![codecov](https://camo.githubusercontent.com/45b5b43ab5e62f473d8a12a29401a32f924d33f3222a6bf93c0438335e9220d7/68747470733a2f2f636f6465636f762e696f2f67682f67656f726765626f6f742f6c61726176656c2d6563686f2d6170692d676174657761792f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d55564941334642515050)](https://codecov.io/gh/georgeboot/laravel-echo-api-gateway)

This package enables you to use API Gateway‘s Websockets as a driver for [Laravel Echo](https://github.com/laravel/echo), so you don’t have to use services like Pusher or Socket.io.

It works by setting up a websocket API in API Gateway, and configure it to invoke a Lambda function, every time a message is sent to the websocket. This package includes and autoconfigures a handler to respond to these websocket messages. We also configure Laravel to use this connection as a broadcast driver.

This package currently only works with either [Bref](https://bref.sh) or [Laravel Vapor](https://vapor.laravel.com), though the latter one involves some manual set-up.

Requirements
------------

[](#requirements)

In order to use this package, your project needs to meet the following criteria:

- PHP 7.4 or 8.x
- Laravel 6 to 11
- Uses either [bref](https://bref.sh) or [Laravel Vapor](https://vapor.laravel.com) to deploy to AWS
- Has a working queue
- Uses Laravel Mix or any other tool to bundle your assets

Installation
------------

[](#installation)

Installation of this package is fairly simply.

First we have to install both the composer and npm package:

```
composer require georgeboot/laravel-echo-api-gateway

yarn add laravel-echo-api-gateway
# or
npm install --save-dev laravel-echo-api-gateway
```

### Platform-specific instructions

[](#platform-specific-instructions)

#### A. When using Bref

[](#a-when-using-bref)

Next, when using Bref, we have to add some elements to our `serverless.yml` file. If using Vapor, these resources have to be created by hand using the AWS CLI or console.

Add a new function that will handle websocket events (messages etc):

```
functions:
    # Add this function
    websocket:
        handler: handlers/websocket.php
        layers:
            - ${bref:layer.php-80}
        events:
            - websocket: $disconnect
            - websocket: $default
```

Add a resource to create and configure our DynamoDB table, where connections will be stored in:

```
resources:
    Resources:
        # Add this resource
        ConnectionsTable:
            Type: AWS::DynamoDB::Table
            Properties:
                TableName: connections
                AttributeDefinitions:
                    - AttributeName: connectionId
                      AttributeType: S
                    - AttributeName: channel
                      AttributeType: S
                KeySchema:
                    - AttributeName: connectionId
                      KeyType: HASH
                    - AttributeName: channel
                      KeyType: RANGE
                GlobalSecondaryIndexes:
                    - IndexName: lookup-by-channel
                      KeySchema:
                          - AttributeName: channel
                            KeyType: HASH
                      Projection:
                          ProjectionType: ALL
                    - IndexName: lookup-by-connection
                      KeySchema:
                          - AttributeName: connectionId
                            KeyType: HASH
                      Projection:
                          ProjectionType: ALL
                BillingMode: PAY_PER_REQUEST
```

Add the following `iamRoleStatement` to enable our Lambda function to access the table:

```
provider:
    name: aws

    iamRoleStatements:
        # Add this iamRoleStatement
        - Effect: Allow
          Action: [ dynamodb:Query, dynamodb:GetItem, dynamodb:PutItem, dynamodb:UpdateItem, dynamodb:DeleteItem, dynamodb:BatchWriteItem ]
          Resource:
              - !GetAtt ConnectionsTable.Arn
              - !Join [ '', [ !GetAtt ConnectionsTable.Arn, '/index/*' ] ]
```

Add an environment variable to autogenerate our websocket URL:

```
provider:
    name: aws

    environment:
        # Add these variables
        # Please note : in Laravel 11, this setting is now BROADCAST_CONNECTION
        BROADCAST_DRIVER: laravel-echo-api-gateway
        LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE: !Ref ConnectionsTable
        LARAVEL_ECHO_API_GATEWAY_API_ID: !Ref WebsocketsApi
        LARAVEL_ECHO_API_GATEWAY_API_STAGE: "${self:provider.stage}"
```

Next, create the PHP handler file in `handlers/websocket.php`

```
