@@ -46,152 +46,152 @@ npn install --save laravel-echo-api-gateway
46
46
<details >
47
47
<summary >Expand Bref-specific instructions</summary >
48
48
49
- #### A. When using Bref
50
-
51
- Next, when using Bref, we have to add some elements to our ` serverless.yml ` file. If using Vapor, these resources have
52
- to be created by hand using the AWS CLI or console.
53
-
54
- Add a new function that will handle websocket events (messages etc):
55
-
56
- ``` yaml
57
- functions :
58
- # Add this function
59
- websocket :
60
- handler : handlers/websocket.php
61
- layers :
62
- - ${bref:layer.php-80}
63
- events :
64
- - websocket : $disconnect
65
- - websocket : $default
66
- ` ` `
67
-
68
- Add a resource to create and configure our DynamoDB table, where connections will be stored in:
69
-
70
- ` ` ` yaml
71
- resources :
72
- Resources :
73
- # Add this resource
74
- ConnectionsTable :
75
- Type : AWS::DynamoDB::Table
76
- Properties :
77
- TableName : connections
78
- AttributeDefinitions :
79
- - AttributeName : connectionId
80
- AttributeType : S
81
- - AttributeName : channel
82
- AttributeType : S
83
- KeySchema :
84
- - AttributeName : connectionId
85
- KeyType : HASH
86
- - AttributeName : channel
87
- KeyType : RANGE
88
- GlobalSecondaryIndexes :
89
- - IndexName : lookup-by-channel
90
- KeySchema :
91
- - AttributeName : channel
92
- KeyType : HASH
93
- Projection :
94
- ProjectionType : ALL
95
- - IndexName : lookup-by-connection
96
- KeySchema :
97
- - AttributeName : connectionId
98
- KeyType : HASH
99
- Projection :
100
- ProjectionType : ALL
101
- BillingMode : PAY_PER_REQUEST
102
- ` ` `
103
-
104
- Add the following ` iamRoleStatement` to enable our Lambda function to access the table:
105
-
106
- ` ` ` yaml
107
- provider:
108
- name: aws
109
-
110
- iamRoleStatements:
111
- # Add this iamRoleStatement
112
- - Effect: Allow
113
- Action: [ dynamodb:Query, dynamodb:GetItem, dynamodb:PutItem, dynamodb:UpdateItem, dynamodb:DeleteItem, dynamodb:BatchWriteItem ]
114
- Resource:
115
- - !GetAtt ConnectionsTable.Arn
116
- - !Join [ '', [ !GetAtt ConnectionsTable.Arn, '/index/*' ] ]
117
- ` ` `
118
-
119
- Add an environment variable to autogenerate our websocket URL :
120
-
121
- ` ` ` yaml
122
- provider:
123
- name: aws
124
-
125
- environment:
126
- # Add these variables
127
- BROADCAST_DRIVER: laravel-echo-api-gateway
128
- LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE: !Ref ConnectionsTable
129
- LARAVEL_ECHO_API_GATEWAY_API_ID: !Ref WebsocketsApi
130
- LARAVEL_ECHO_API_GATEWAY_API_STAGE: "${self:provider.stage}"
131
- ` ` `
132
-
133
- Next, create the PHP handler file in `handlers/websocket.php`
134
-
135
- ` ` ` php
136
- <?php
137
-
138
- use Georgeboot\L aravelEchoApiGateway\H andler;
139
- use Illuminate\F oundation\A pplication;
140
-
141
- require __DIR__ . '/../vendor/autoload.php';
142
-
143
- /** @var Application $app */
144
- $app = require __DIR__ . '/../bootstrap/app.php';
145
-
146
- $kernel = $app->make(Illuminate\C ontracts\C onsole\K ernel::class);
147
- $kernel->bootstrap();
148
-
149
- return $app->make(Handler::class);
150
- ` ` `
151
-
152
- Now, deploy your app by running `serverless deploy` or similar. Write down the websocket url the output gives you.
49
+ #### A. When using Bref
50
+
51
+ Next, when using Bref, we have to add some elements to our `serverless.yml` file. If using Vapor, these resources have
52
+ to be created by hand using the AWS CLI or console.
53
+
54
+ Add a new function that will handle websocket events (messages etc):
55
+
56
+ ```yaml
57
+ functions:
58
+ # Add this function
59
+ websocket:
60
+ handler: handlers/websocket.php
61
+ layers:
62
+ - ${bref:layer.php-80}
63
+ events:
64
+ - websocket: $disconnect
65
+ - websocket: $default
66
+ ```
67
+
68
+ Add a resource to create and configure our DynamoDB table, where connections will be stored in:
69
+
70
+ ```yaml
71
+ resources:
72
+ Resources:
73
+ # Add this resource
74
+ ConnectionsTable:
75
+ Type: AWS::DynamoDB::Table
76
+ Properties:
77
+ TableName: connections
78
+ AttributeDefinitions:
79
+ - AttributeName: connectionId
80
+ AttributeType: S
81
+ - AttributeName: channel
82
+ AttributeType: S
83
+ KeySchema:
84
+ - AttributeName: connectionId
85
+ KeyType: HASH
86
+ - AttributeName: channel
87
+ KeyType: RANGE
88
+ GlobalSecondaryIndexes:
89
+ - IndexName: lookup-by-channel
90
+ KeySchema:
91
+ - AttributeName: channel
92
+ KeyType: HASH
93
+ Projection:
94
+ ProjectionType: ALL
95
+ - IndexName: lookup-by-connection
96
+ KeySchema:
97
+ - AttributeName: connectionId
98
+ KeyType: HASH
99
+ Projection:
100
+ ProjectionType: ALL
101
+ BillingMode: PAY_PER_REQUEST
102
+ ```
103
+
104
+ Add the following `iamRoleStatement` to enable our Lambda function to access the table:
105
+
106
+ ```yaml
107
+ provider:
108
+ name: aws
109
+
110
+ iamRoleStatements:
111
+ # Add this iamRoleStatement
112
+ - Effect: Allow
113
+ Action: [ dynamodb:Query, dynamodb:GetItem, dynamodb:PutItem, dynamodb:UpdateItem, dynamodb:DeleteItem, dynamodb:BatchWriteItem ]
114
+ Resource:
115
+ - !GetAtt ConnectionsTable.Arn
116
+ - !Join [ '', [ !GetAtt ConnectionsTable.Arn, '/index/*' ] ]
117
+ ```
118
+
119
+ Add an environment variable to autogenerate our websocket URL:
120
+
121
+ ```yaml
122
+ provider:
123
+ name: aws
124
+
125
+ environment:
126
+ # Add these variables
127
+ BROADCAST_DRIVER: laravel-echo-api-gateway
128
+ LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE: !Ref ConnectionsTable
129
+ LARAVEL_ECHO_API_GATEWAY_API_ID: !Ref WebsocketsApi
130
+ LARAVEL_ECHO_API_GATEWAY_API_STAGE: "${self:provider.stage}"
131
+ ```
132
+
133
+ Next, create the PHP handler file in `handlers/websocket.php`
134
+
135
+ ```php
136
+ <?php
137
+
138
+ use Georgeboot\LaravelEchoApiGateway\Handler;
139
+ use Illuminate\Foundation\Application;
140
+
141
+ require __DIR__ . '/../vendor/autoload.php';
142
+
143
+ /** @var Application $app */
144
+ $app = require __DIR__ . '/../bootstrap/app.php';
145
+
146
+ $kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
147
+ $kernel->bootstrap();
148
+
149
+ return $app->make(Handler::class);
150
+ ```
151
+
152
+ Now, deploy your app by running `serverless deploy` or similar. Write down the websocket url the output gives you.
153
153
154
154
</details >
155
155
156
156
<details >
157
- <summary>Expand Vapor-specific instructions</summary>
158
-
159
- # ### B. When using Vapor
160
-
161
- When using Vapor, you will have to create these required resources by hand using the AWS CLI or Console :
162
-
163
- # #### B1. DynamoDB table for connections
164
-
165
- Create a DynamoDB table for the connections. Use `connectionId` (string) as a HASH key, and `channel` (string) as a SORT
166
- key. Set the capacity setting to whatever you like (probably on-demand).
167
-
168
- Create 2 indexes :
169
-
170
- 1. Name : ` lookup-by-connection` , key: `connectionId`, no sort key, projected: ALL
171
- 2. Name : ` lookup-by-channel` , key: `channel`, no sort key, projected: ALL
172
-
173
- # #### B2. API Gateway
174
-
175
- Create a new Websocket API. Enter a name and leave the route selection expression to what it is. Add a `$disconnect`
176
- and `$default`. Set both integrations to `Lambda` and select your CLI lambda from the list. Set the name of the stage to
177
- what you desire and create the API. Once created, write down the ID, as we'll need it later.
178
-
179
- # #### B3. IAM Permissions
180
-
181
- In IAM, go to roles and open `laravel-vapor-role`. Open the inline policy and edit it. On the JSON tab,
182
- add `"execute-api:*"` to the list of actions.
183
-
184
- Then, login to [Laravel Vapor](https://vapor.laravel.com/app), go to team settings, AWS Accounts, click on Role next to
185
- the correct account and deselect Receive Updates.
186
-
187
- Edit your `.env` :
188
-
189
- ` ` ` dotenv
190
- BROADCAST_DRIVER=laravel-echo-api-gateway
191
- LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE=the-table-name-you-entered-when-creating-it
192
- LARAVEL_ECHO_API_GATEWAY_API_ID=your-websocket-api-id
193
- LARAVEL_ECHO_API_GATEWAY_API_STAGE=your-api-stage-name
194
- ` ` `
157
+ <summary >Expand Vapor-specific instructions</summary >
158
+
159
+ #### B. When using Vapor
160
+
161
+ When using Vapor, you will have to create these required resources by hand using the AWS CLI or Console:
162
+
163
+ ##### B1. DynamoDB table for connections
164
+
165
+ Create a DynamoDB table for the connections. Use `connectionId` (string) as a HASH key, and `channel` (string) as a SORT
166
+ key. Set the capacity setting to whatever you like (probably on-demand).
167
+
168
+ Create 2 indexes:
169
+
170
+ 1. Name: `lookup-by-connection`, key: `connectionId`, no sort key, projected: ALL
171
+ 2. Name: `lookup-by-channel`, key: `channel`, no sort key, projected: ALL
172
+
173
+ ##### B2. API Gateway
174
+
175
+ Create a new Websocket API. Enter a name and leave the route selection expression to what it is. Add a `$disconnect`
176
+ and `$default`. Set both integrations to `Lambda` and select your CLI lambda from the list. Set the name of the stage to
177
+ what you desire and create the API. Once created, write down the ID, as we'll need it later.
178
+
179
+ ##### B3. IAM Permissions
180
+
181
+ In IAM, go to roles and open `laravel-vapor-role`. Open the inline policy and edit it. On the JSON tab,
182
+ add `"execute-api:*"` to the list of actions.
183
+
184
+ Then, login to [Laravel Vapor](https://vapor.laravel.com/app), go to team settings, AWS Accounts, click on Role next to
185
+ the correct account and deselect Receive Updates.
186
+
187
+ Edit your `.env`:
188
+
189
+ ```dotenv
190
+ BROADCAST_DRIVER=laravel-echo-api-gateway
191
+ LARAVEL_ECHO_API_GATEWAY_DYNAMODB_TABLE=the-table-name-you-entered-when-creating-it
192
+ LARAVEL_ECHO_API_GATEWAY_API_ID=your-websocket-api-id
193
+ LARAVEL_ECHO_API_GATEWAY_API_STAGE=your-api-stage-name
194
+ ```
195
195
196
196
</details >
197
197
0 commit comments