Skip to content

Commit 497592d

Browse files
committed
Readme changes
1 parent 5db463c commit 497592d

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

README.md

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ yarn add laravel-echo-api-gateway
4141
npn install --save laravel-echo-api-gateway
4242
```
4343

44+
### When using Bref
45+
4446
Next, when using Bref, we have to add some elements to our `serverless.yml` file. If using Vapor, these resources have
4547
to 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

123128
Next, create the PHP handler file in `handlers/websocket.php`
@@ -139,13 +144,47 @@ $kernel->bootstrap();
139144
return $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+
142177
Edit your `.env`:
143178

144179
```dotenv
145180
BROADCAST_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+
149188
Add to your javascript file:
150189

151190
```js
@@ -154,7 +193,8 @@ import {broadcaster} from 'laravel-echo-api-gateway';
154193
155194
const 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

config/laravel-echo-api-gateway.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
'secret' => env('AWS_SECRET_ACCESS_KEY'),
88
'token' => env('AWS_SESSION_TOKEN'),
99
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
10-
'endpoint' => env('DYNAMODB_ENDPOINT'),
1110
],
1211

13-
'table' => env('LARAVEL_ECHO_API_GATEWAY_TABLE', 'connections'),
12+
'api' => [
13+
'id' => env('LARAVEL_ECHO_API_GATEWAY_API_ID'),
14+
'stage' => env('LARAVEL_ECHO_API_GATEWAY_API_STAGE'),
15+
],
1416

15-
'endpoint' => env('BROADCAST_API_GATEWAY_URL'),
17+
'dynamodb' => [
18+
'endpoint' => env('DYNAMODB_ENDPOINT'),
19+
'table' => env('LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE', 'connections'),
20+
],
1621

1722
];

src/ConnectionRepository.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Georgeboot\LaravelEchoApiGateway;
44

55
use Aws\ApiGatewayManagementApi\ApiGatewayManagementApiClient;
6-
use Illuminate\Support\Str;
76

87
class ConnectionRepository
98
{
@@ -13,7 +12,7 @@ public function __construct(array $config)
1312
{
1413
$this->apiGatewayManagementApiClient = new ApiGatewayManagementApiClient(array_merge($config['connection'], [
1514
'version' => '2018-11-29',
16-
'endpoint' => Str::replaceFirst('wss://', 'https://', $config['endpoint']) . '/',
15+
'endpoint' => "https://{$config['api']['id']}.execute-api.{$config['connection']['region']}.amazonaws.com/{$config['api']['stage']}/",
1716
]));
1817
}
1918

src/SubscriptionRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public function __construct(array $config)
1616
{
1717
$this->dynamoDb = new DynamoDbClient(array_merge($config['connection'], [
1818
'version' => '2012-08-10',
19+
'endpoint' => $config['dynamodb']['endpoint'],
1920
]));
2021

21-
$this->table = $config['table'];
22+
$this->table = $config['dynamodb']['table'];
2223
}
2324

2425
public function getConnectionIdsForChannel(string ...$channels): Collection

0 commit comments

Comments
 (0)