Skip to content
Our Sponsors
Open in Anthropic

WebSocket ​

Eden Treaty supports WebSocket using the subscribe method.

typescript
import { Elysia, t } from "elysia";
import { treaty } from "@elysia/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");
});

.subscribe accepts the same parameters as get and head.

Response ​

Eden.subscribe returns EdenWS which extends the WebSocket, resulting in identical syntax.

If more control is needed, EdenWebSocket.raw can be accessed to interact with the native WebSocket API.