You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 20, 2025. It is now read-only.
Co-authored-by: Jye Cusch <[email protected]>
Simplify project so it doesn't need an update to the nitric.yaml config.
Simplify project so it doesn't need an update to the nitric.yaml config.
handle errors and add consistency between guides
Copy file name to clipboardExpand all lines: docs/guides/go/realtime-messaging.mdx
+32-34Lines changed: 32 additions & 34 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,15 @@
1
1
---
2
-
title_seo: Building your first WebSocket Application with Go and Nitric
3
2
description: Use the Nitric framework to easily build and deploy Go WebSocket applications for AWS.
4
3
tags:
5
4
- Realtime & Websockets
6
5
- Key Value Store
7
6
---
8
7
9
-
# Building your first WebSocket Application with Nitric
8
+
# Building a chat app in Go with WebSockets and Nitric
10
9
11
10
## What we'll be doing
12
11
13
-
1. Use Nitric to create a WebSocket endpoint
12
+
1. Use Nitric to create a WebSocket API
14
13
2. Manage WebSocket connections using a Key-Value store
15
14
3. Handle WebSocket events:
16
15
- Register connections on connect
@@ -23,20 +22,20 @@ tags:
23
22
24
23
-[Go](https://go.dev/dl/)
25
24
- The [Nitric CLI](/get-started/installation)
26
-
- An [AWS](https://aws.amazon.com) account
25
+
- An [AWS](https://aws.amazon.com) account (optional)
27
26
28
27
## Getting started
29
28
30
29
We'll start by creating a new project for our WebSocket application.
31
30
32
31
```bash
33
-
nitric new my-websocket-app go-starter
32
+
nitric new websocket-app go-starter
34
33
```
35
34
36
35
Next, open the project in your editor of choice.
37
36
38
37
```bash
39
-
cdmy-websocket-app
38
+
cd websocket-app
40
39
```
41
40
42
41
Make sure all dependencies are resolved:
@@ -69,9 +68,9 @@ If everything is working as expected, you can now delete all files/folders in th
69
68
70
69
## Building the WebSocket Application
71
70
72
-
Let's begin by setting up the WebSocket application. First, create a new folder called `websockets` within the services directory. Inside this folder, add a file named `main.go`, and include the following code:
71
+
Let's begin by setting up the WebSocket application. Add a file named `main.go` to your 'services/websockets' folder, and include the following code:
73
72
74
-
```go
73
+
```go title:services/websockets/main.go
75
74
package main
76
75
77
76
import (
@@ -84,7 +83,7 @@ import (
84
83
)
85
84
86
85
funcmain() {
87
-
// Create a WebSocket endpoint named "public".
86
+
// Create a WebSocket API named "public".
88
87
ws:= nitric.NewWebsocket("public")
89
88
90
89
// Initialize a KV store named "connections" with Get, Set, and Delete permissions.
@@ -98,8 +97,8 @@ func main() {
98
97
99
98
Here we're creating:
100
99
101
-
- A [WebSocket](/websockets)endpoint named `public`
102
-
- A [Key-Value store](/keyvalue) named `connections` to track WebSocket connections
100
+
- A [WebSocket](/websockets)API named `public`
101
+
- A [Key-Value store](/keyvalue) named `connections` to track client connections
103
102
104
103
From here, let's add some features to that function that allow us to manage connections and broadcast messages.
105
104
@@ -112,10 +111,11 @@ From here, let's add some features to that function that allow us to manage conn
fmt.Println("Error sending message to connection ID", connectionId, ":", err)
227
234
return
228
235
}
229
236
}
@@ -239,36 +246,27 @@ Do a quick `go mod tidy` to make sure all new dependencies are resolved.
239
246
240
247
## Ok, let's run this thing!
241
248
242
-
Now that you have your WebSocket application defined with handlers for each event, it's time to test it locally. Update your `nitric.yaml` to point to the service at `websockets/main.go`:
243
-
244
-
```yaml
245
-
services:
246
-
- match: websockets/*
247
-
runtime: go
248
-
start: go run ./$SERVICE_PATH
249
-
```
249
+
Now that you have your WebSocket application defined with handlers for each event, it's time to test it locally.
250
250
251
251
```bash
252
252
nitric start
253
253
```
254
254
255
255
Once it starts, the application will be ready to accept WebSocket connections. You can use a WebSocket client like Postman or any other WebSocket tool to test the application. Alternatively, you can use the [Nitric Dashboard](/get-started/foundations/projects/local-development#local-dashboard).
256
256
257
-
We will keep it running for our tests.
258
-
259
257
## Deploy to the cloud
260
258
261
-
At this point, you can deploy what you've built to any of the supported cloud providers. To do this, start by setting up your credentials and configuration for [AWS](/providers/pulumi/aws).
259
+
At this point, you can deploy what you've built to any of the supported cloud providers. In this example we'll deploy to AWS. Start by setting up your credentials and configuration for the [nitric/aws provider](/providers/pulumi/aws).
262
260
263
-
Next, we'll need to create a `stack`. A stack represents a deployed instance of an application, which is a key value store of resources defined in your project. You might want separate stacks for each environment, such as stacks for `dev`, `test`, and `prod`. For now, let's start by creating a `dev` stack.
261
+
Next, we'll need to create a `stack file` (deployment target). A stack is a deployed instance of an application. You might want separate stacks for each environment, such as stacks for `dev`, `test`, and `prod`. For now, let's start by creating a file for the`dev` stack.
264
262
265
263
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
266
264
267
265
```bash
268
266
nitric stack new dev aws
269
267
```
270
268
271
-
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region. Let's use`us-east-1`.
269
+
Edit the stack file `nitric.dev.yaml` and set your preferred AWS region, for example`us-east-1`.
272
270
273
271
### AWS
274
272
@@ -277,7 +275,7 @@ Continue by checking your stack file `nitric.dev.yaml` and adding in your prefer
277
275
costs associated with deployment.
278
276
</Note>
279
277
280
-
We called our stack `dev`. Let's try deploying it with the `up` command:
278
+
Let's try deploying the stack with the `up` command:
The Nitric Terraform providers are currently in preview, to enable them you'll need to enable beta-providers in your Nitric project. You can do this by adding the following to your project's `nitric.yaml` file:
133
133
134
-
```
134
+
```yaml title:nitric.yaml
135
135
preview:
136
136
- beta-providers
137
137
```
138
138
139
-
Once you've created your Nitric stack, you can generate the Terraform code by running the following command:
139
+
Once you've created your stack file, you can generate the Terraform code by running the following command:
140
140
141
141
```
142
142
nitric up
143
143
```
144
144
145
-
This will generate the Terraform code for your Nitric application into a folder named `cdktf.out` by default.
145
+
This will generate Terraform code which can deploy your application. The output will be in a folder named `cdktf.out` by default.
0 commit comments