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

Commit 04e52ca

Browse files
committed
Adds websocket proxy support to websites
1 parent ed98a97 commit 04e52ca

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

docs/architecture/websites.mdx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: 'Learn how Nitric provisions and manages websites with Terraform an
1010

1111
- App code uses the [Website resource](/websites) defined in `nitric.yaml` to specify the websites and their configurations.
1212
- Developers can configure base path, index pages, error pages, and other settings in their `nitric.yaml` file.
13-
- When a website is defined, a single CDN endpoint is automatically created to serve the website content globally, including APIs as proxies.
13+
- When a website is defined, a single CDN endpoint is automatically created to serve the website content globally, including APIs and websockets as proxies.
1414

1515
**Operations** use default or overridden IaC (e.g., Terraform modules) to provision the necessary resources for their target cloud.
1616

@@ -34,6 +34,7 @@ flowchart TD
3434
NitricUp -->|Create CDN| CloudFront
3535
CloudFront -->|Serve Static Files| S3
3636
CloudFront -->|Rewrite /api/* to API| Rewrite
37+
CloudFront -->|Rewrite /ws/* to Websocket| Rewrite
3738
3839
classDef default line-height:1;
3940
classDef edgeLabel line-height:2;
@@ -59,6 +60,7 @@ flowchart TD
5960
NitricUp -->|Create CDN| FrontDoor
6061
FrontDoor -->|Serve Static Files| Storage
6162
FrontDoor -->|Rewrite /api/* to API| Rewrite
63+
FrontDoor -->|Rewrite /ws/* to Websocket| Rewrite
6264
6365
classDef default line-height:1;
6466
classDef edgeLabel line-height:2;
@@ -122,15 +124,25 @@ websites:
122124
url: http://localhost:4322
123125
```
124126
125-
### Access an API from the website frontend
127+
### Access an API or Websocket from the website frontend
126128
127129
```javascript
130+
// API Example
128131
async function fetchData() {
129132
// due to apis being served from the same domain thanks to rewrites, no CORS is required
130133
const response = await fetch('/api/main/hello')
131134
const data = await response.json()
132135
console.log(data)
133136
}
137+
138+
// Websocket Example
139+
const ws = new WebSocket('/ws/chat')
140+
141+
ws.onmessage = (event) => {
142+
console.log(event.data)
143+
}
144+
145+
ws.send('Hello, server!')
134146
```
135147

136148
**Operations** will use or extend the Nitric infrastructure modules, including both Terraform and Pulumi:

docs/guides/deno/byo-deep-research.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
title_seo: 'Building Your Own Deep Research System with Nitric'
33
description: 'Learn how to build a simple research system using Nitric that can be tested locally and deployed to production'
4-
author: tim_holm
54
languages:
65
- typescript
76
tags:

docs/websites.mdx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,37 +159,58 @@ However, client-side routes (like React Router) are not detected at deployment t
159159
The following paths are reserved for use by the Nitric framework and cannot be used as website paths:
160160

161161
- `/api` - used to rewrite API requests
162+
- `/ws` - used to rewrite websocket requests
162163

163164
We are looking at making the **rewrite path configurable** in the future, so you will have more flexibility in defining your own path structure.
164165

165-
## API Routes
166+
## API and Websocket Routes
166167

167-
If you have at least **one website enabled**, Nitric automatically serves your API under the `/api` path.
168+
When you have at least **one website enabled**, Nitric automatically serves your API under the `/api` path and websocket routes under the `/ws` path.
169+
170+
### Accessing Routes
168171

169172
You can access your API routes at:
170173

171174
```bash
172175
https://<your-cdn-endpoint>/api/<your-api>/<your-route>
173176
```
174177

175-
### When Are API Routes Enabled?
178+
And connect to websocket routes at:
179+
180+
```bash
181+
https://<your-cdn-endpoint>/ws/<your-websocket>
182+
```
183+
184+
### When Are Routes Enabled?
176185

177-
API route rewrites are **only enabled if you have at least one website defined** in your project.
178-
We are exploring options to allow enabling API routing **without requiring a website** in the future.
186+
Both API and websocket route rewrites are **only enabled if you have at least one website defined** in your project.
187+
We are exploring options to allow enabling these routes **without requiring a website** in the future.
179188

180189
### Why This Approach?
181190

182-
By serving your API under the same domain as your website, you avoid **CORS issues** and can use **relative paths** to access your API directly from your frontend code.
191+
By serving your API and websocket routes under the same domain as your website, you avoid **CORS issues** and can use **relative paths** to access your routes directly from your frontend code.
183192

184-
### Example API Call
193+
### Example Usage
194+
195+
Here's how to use both API and websocket routes in your frontend code:
185196

186197
```javascript
198+
// API Example
187199
async function fetchData() {
188200
// due to apis being served from the same domain thanks to rewrites, no CORS is required
189201
const response = await fetch('/api/main/hello')
190202
const data = await response.json()
191203
console.log(data)
192204
}
205+
206+
// Websocket Example with a websocket named `chat`
207+
const ws = new WebSocket('/ws/chat')
208+
209+
ws.onmessage = (event) => {
210+
console.log(event.data)
211+
}
212+
213+
ws.send('Hello, server!')
193214
```
194215

195216
## Support for Frameworks like Next.js

0 commit comments

Comments
 (0)