Skip to content
Our Sponsors
Open in Anthropic

WebSocket

Eden Treaty는 subscribe 메서드를 사용하여 WebSocket을 지원합니다.

typescript
import { 
Elysia
,
t
} from "elysia";
import {
treaty
} from "@elysiajs/eden";
const
app
= new
Elysia
()
.
ws
("/chat", {
body
:
t
.
String
(),
response
:
t
.
String
(),
message
(
ws
,
message
) {
ws
.
send
(
message
);
}, }) .
listen
(3000);
const
api
=
treaty
<typeof
app
>("localhost:3000");
const
chat
=
api
.
chat
.
subscribe
();
chat
.
subscribe
((
message
) => {
console
.
log
("got",
message
);
});
chat
.
on
("open", () => {
chat
.
send
("hello from client");
});

.subscribegethead와 동일한 매개변수를 받습니다.

Response

Eden.subscribeWebSocket을 확장하는 EdenWS를 반환하여 동일한 문법을 제공합니다.

더 많은 제어가 필요한 경우, EdenWebSocket.raw에 액세스하여 네이티브 WebSocket API와 상호 작용할 수 있습니다.