WebSocket
Eden Treaty admite WebSocket con el método subscribe.
import { Elysia, t } from "elysia";import { websocket } from 'elysia/websocket'import { treaty } from "@elysia/eden";
const app = new Elysia() .use(websocket()) .ws("/chat", { body: t.String(), response: t.String(), message({ body }) { return body }, }) .listen(3000);
const api = treaty<typeof app>("localhost:3000");
const chat = api.chat.subscribe();
chat.subscribe((message) => { console.log("got", message.data);});
chat.on("open", () => { chat.send("hello from client");});subscribe() acepta parámetros de query en sus opciones. Los WebSockets del navegador no pueden enviar cualquier header en la petición; usa cookies o un flujo de autenticación que acepte el servidor.
Respuesta
Sección titulada «Respuesta»subscribe() devuelve un wrapper EdenWS, con los helpers tipados send, subscribe, on, off y close.
Usa chat.ws para acceder al WebSocket nativo que hay debajo. El wrapper no es una subclase con todas las propiedades del WebSocket nativo.