Skip to content

Commit 96bc1e9

Browse files
authored
docs: unix socket add-on (#4587)
* init * removed unneeded examples * minor addons
1 parent 2c3ac34 commit 96bc1e9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/examples/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,41 @@ const defaultDispatcher = new Agent({
147147

148148
setGlobalDispatcher(defaultDispatcher) // Add these interceptors to all `fetch` and Undici `request` calls
149149
```
150+
151+
## Connecting via Unix domain sockets (UDS)
152+
153+
### request() over UDS (per-call dispatcher)
154+
```js
155+
const { Agent, request } = require('undici')
156+
157+
async function requestOverUds () {
158+
const uds = new Agent({ connect: { socketPath: '/var/run/docker.sock' } })
159+
try {
160+
const { statusCode, headers, body } = await request('http://localhost/_ping', {
161+
dispatcher: uds
162+
})
163+
console.log(statusCode, headers, await body.text())
164+
} finally {
165+
await uds.close()
166+
}
167+
}
168+
```
169+
170+
### fetch() over UDS (per-call dispatcher)
171+
```js
172+
const { Agent, fetch } = require('undici')
173+
174+
async function fetchOverUds () {
175+
const uds = new Agent({ connect: { socketPath: '/var/run/docker.sock' } })
176+
try {
177+
const res = await fetch('http://localhost/containers/json', { dispatcher: uds })
178+
console.log(res.status, await res.text())
179+
} finally {
180+
await uds.close()
181+
}
182+
}
183+
```
184+
185+
> Note
186+
> - `connect.socketPath` must be the exact filesystem path your server listens on (e.g., Docker: `/var/run/docker.sock`)..
187+
> - Not supported on Windows (uses named pipes instead).

0 commit comments

Comments
 (0)