@@ -41,6 +41,8 @@ yarn add laravel-echo-api-gateway
4141npn install --save laravel-echo-api-gateway
4242```
4343
44+ ### When using Bref
45+
4446Next, when using Bref, we have to add some elements to our ` serverless.yml ` file. If using Vapor, these resources have
4547to be created by hand using the AWS CLI or console.
4648
@@ -116,8 +118,11 @@ provider:
116118 name: aws
117119
118120 environment:
119- # Add this line
120- BROADCAST_API_GATEWAY_URL: !Join [ '', [ 'wss://', !Ref "WebsocketsApi", '.execute-api.', "${self:provider.region}", '.', !Ref "AWS::URLSuffix", '/', "${self:provider.stage}" ] ]
121+ # Add these variables
122+ BROADCAST_DRIVER: laravel-echo-api-gateway
123+ LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE: !Ref ConnectionsTable
124+ LARAVEL_ECHO_API_GATEWAY_API_ID: !Ref WebsocketsApi
125+ LARAVEL_ECHO_API_GATEWAY_API_STAGE: "${self:provider.stage}"
121126` ` `
122127
123128Next, create the PHP handler file in `handlers/websocket.php`
@@ -139,13 +144,47 @@ $kernel->bootstrap();
139144return $app->make(Handler::class);
140145` ` `
141146
147+ Now, deploy your app by running `serverless deploy` or similar. Write down the websocket url the output gives you.
148+
149+ # ## When using Vapor
150+
151+ When using Vapor, you will have to create these required resources by hand using the AWS CLI or Console :
152+
153+ # ### DynamoDB table for connections
154+
155+ Create a DynamoDB table for the connections. Use `connectionId` (string) as a HASH key, and `channel` (string) as a SORT
156+ key. Set the capacity setting to whatever you like (probably on-demand).
157+
158+ Create 2 indexes :
159+
160+ 1. Name : ` lookup-by-connection` , key: `connectionId`, no sort key, projected: ALL
161+ 2. Name : ` lookup-by-channel` , key: `channel`, no sort key, projected: ALL
162+
163+ # ### API Gateway
164+
165+ Create a new Websocket API. Enter a name and leave the route selection expression to what it is. Add a `$disconnect`
166+ and `$default`. Set both integrations to `Lambda` and select your CLI lambda from the list. Set the name of the stage to
167+ what you desire and create the API. Once created, write down the ID, as we'll need it later.
168+
169+ # ### IAM Permissions
170+
171+ In IAM, go to roles and open `laravel-vapor-role`. Open the inline policy and edit it. On the JSON tab,
172+ add `"execute-api:*"` to the list of actions.
173+
174+ Then, login to [Laravel Vapor](https://vapor.laravel.com/app), go to team settings, AWS Accounts, click on Role next to
175+ the correct account and deselect Receive Updates.
176+
142177Edit your `.env` :
143178
144179` ` ` dotenv
145180BROADCAST_DRIVER=laravel-echo-api-gateway
146- MIX_BROADCAST_API_GATEWAY_URL="${BROADCAST_API_GATEWAY_URL}"
181+ LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE=the-table-name-you-entered-when-creating-it
182+ LARAVEL_ECHO_API_GATEWAY_API_ID=your-websocket-api-id
183+ LARAVEL_ECHO_API_GATEWAY_API_STAGE=your-api-stage-name
147184` ` `
148185
186+ # ## For both Bref and Vapor
187+
149188Add to your javascript file :
150189
151190` ` ` js
@@ -154,7 +193,8 @@ import {broadcaster} from 'laravel-echo-api-gateway';
154193
155194const echo = new Echo({
156195 broadcaster,
157- host: process.env.MIX_BROADCAST_API_GATEWAY_URL,
196+ // replace the placeholders
197+ host: 'wss://{api-ip}.execute-api.{region}.amazonaws.com/{stage}',
158198});
159199` ` `
160200
0 commit comments