Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 20577fc

Browse files
committed
handle errors and add consistency between guides
1 parent 1591d32 commit 20577fc

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

docs/guides/go/realtime-messaging.mdx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ tags:
2929
We'll start by creating a new project for our WebSocket application.
3030

3131
```bash
32-
nitric new my-websocket-app go-starter
32+
nitric new websocket-app go-starter
3333
```
3434

3535
Next, open the project in your editor of choice.
3636

3737
```bash
38-
cd my-websocket-app
38+
cd websocket-app
3939
```
4040

4141
Make sure all dependencies are resolved:
@@ -111,10 +111,11 @@ From here, let's add some features to that function that allow us to manage conn
111111

112112
```go
113113
ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
114-
err := connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
114+
err := connections.Set(context.Background(), ctx.Request.ConnectionID(), map[string]interface{}{
115115
"connectionId": ctx.Request.ConnectionID(),
116116
})
117117
if err != nil {
118+
fmt.Println("Error storing connection ID in KV store:", err)
118119
return
119120
}
120121
})
@@ -126,6 +127,7 @@ ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
126127
ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
127128
err := connections.Delete(context.Background(), ctx.Request.ConnectionID())
128129
if err != nil {
130+
fmt.Println("Error deleting connection ID in KV store:", err)
129131
return
130132
}
131133
})
@@ -135,8 +137,9 @@ ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
135137

136138
```go
137139
ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
138-
connectionStream, err := connections.Keys(context.TODO())
140+
connectionStream, err := connections.Keys(context.Background())
139141
if err != nil {
142+
fmt.Println("Error retrieving connection keys from KV store:", err)
140143
return
141144
}
142145

@@ -155,6 +158,7 @@ ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
155158
message := fmt.Sprintf("%s: %s", senderId, ctx.Request.Message())
156159
err = ws.Send(context.Background(), connectionId, []byte(message))
157160
if err != nil {
161+
fmt.Println("Error sending message to connection ID", connectionId, ":", err)
158162
return
159163
}
160164
}
@@ -187,24 +191,27 @@ func main() {
187191

188192
// Handle new WebSocket connections by storing the connection ID in the KV store.
189193
ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
190-
err := connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
194+
err := connections.Set(context.Background(), ctx.Request.ConnectionID(), map[string]interface{}{
191195
"connectionId": ctx.Request.ConnectionID(),
192196
})
193197
if err != nil {
198+
fmt.Println("Error storing connection ID in KV store:", err)
194199
return
195200
}
196201
})
197202

198203
ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
199-
err := connections.Delete(context.TODO(), ctx.Request.ConnectionID())
204+
err := connections.Delete(context.Background(), ctx.Request.ConnectionID())
200205
if err != nil {
206+
fmt.Println("Error deleting connection ID in KV store:", err)
201207
return
202208
}
203209
})
204210

205211
ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
206-
connectionStream, err := connections.Keys(context.TODO())
212+
connectionStream, err := connections.Keys(context.Background())
207213
if err != nil {
214+
fmt.Println("Error retrieving connection keys from KV store:", err)
208215
return
209216
}
210217

@@ -221,8 +228,9 @@ func main() {
221228
}
222229

223230
message := fmt.Sprintf("%s: %s", senderId, ctx.Request.Message())
224-
err = ws.Send(context.TODO(), connectionId, []byte(message))
231+
err = ws.Send(context.Background(), connectionId, []byte(message))
225232
if err != nil {
233+
fmt.Println("Error sending message to connection ID", connectionId, ":", err)
226234
return
227235
}
228236
}

docs/guides/terraform/terratest.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ We intend to deploy to AWS and will use Terratest to ensure that the following:
3737
Let's start by creating a new project for our application.
3838

3939
```bash
40-
nitric new my-websocket-app go-starter
40+
nitric new websocket-app go-starter
4141
```
4242

4343
### Application code
@@ -187,7 +187,7 @@ import (
187187
func TestTerraformResources(t *testing.T) {
188188
// Set Terraform options, specifying the directory with your Terraform configuration
189189
terraformOptions := &terraform.Options{
190-
TerraformDir: "../cdktf.out/stacks/my-websocket-app-dev",
190+
TerraformDir: "../cdktf.out/stacks/websocket-app-dev",
191191
}
192192
193193
// Ensure resources are destroyed after test completion
@@ -209,11 +209,11 @@ func TestTerraformResources(t *testing.T) {
209209
210210
// Test IAM role creation
211211
iamClient := iam.New(sess)
212-
roleName := "my-websocket-app_websockets-main" // Name of the IAM role to check
212+
roleName := "websocket-app_websockets-main" // Name of the IAM role to check
213213
_, err = iamClient.GetRole(&iam.GetRoleInput{
214214
RoleName: aws.String(roleName),
215215
})
216-
assert.NoError(t, err, "Expected IAM role 'my-websocket-app_websockets-main' to be created")
216+
assert.NoError(t, err, "Expected IAM role 'websocket-app_websockets-main' to be created")
217217
218218
// Test API gateway webSocket creation
219219
apiClient := apigatewayv2.New(sess)

0 commit comments

Comments
 (0)