Skip to content

Commit 30267a6

Browse files
authored
Add plain TCP example to getTcpPort container doc (#23299)
1 parent d3df144 commit 30267a6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/content/docs/durable-objects/api/container.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,21 @@ const res = await port.fetch("http://container/set-state", {
126126
});
127127
```
128128

129+
```js
130+
const conn = this.ctx.container.getTcpPort(8080).connect('10.0.0.1:8080');
131+
await conn.opened;
132+
133+
try {
134+
if (request.body) {
135+
await request.body.pipeTo(conn.writable);
136+
}
137+
return new Response(conn.readable);
138+
} catch (err) {
139+
console.error("Request body piping failed:", err);
140+
return new Response("Failed to proxy request body", { status: 502 });
141+
}
142+
```
143+
129144
#### Parameters
130145

131146
- `port` (number): a TCP port number to use for communication with the container.

0 commit comments

Comments
 (0)