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
+36-34Lines changed: 36 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,31 @@ 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
-
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).
255
+
Once it starts, the application will be ready to accept WebSocket connections that you can easily test with the [Nitric Dashboard](/get-started/foundations/projects/local-development#local-dashboard). You can find the URL to the dashboard in the terminal running the Nitric CLI, by default, it is set to - http://localhost:49152.
The dashboard will show you the WebSocket URL and allow you to connect as a client to send, receive and monitor messages.
258
260
259
261
## Deploy to the cloud
260
262
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).
263
+
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
264
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.
265
+
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
266
265
267
The `stack new` command below will create a stack named `dev` that uses the `aws` provider.
266
268
267
269
```bash
268
270
nitric stack new dev aws
269
271
```
270
272
271
-
Continue by checking your stack file `nitric.dev.yaml` and adding in your preferred region. Let's use`us-east-1`.
273
+
Edit the stack file `nitric.dev.yaml` and set your preferred AWS region, for example`us-east-1`.
272
274
273
275
### AWS
274
276
@@ -277,7 +279,7 @@ Continue by checking your stack file `nitric.dev.yaml` and adding in your prefer
277
279
costs associated with deployment.
278
280
</Note>
279
281
280
-
We called our stack `dev`. Let's try deploying it with the `up` command:
282
+
Let's try deploying the stack with the `up` command:
0 commit comments