pi-mono/packages/chord/test/examples/45-plugin-socket/plugins/echo/server.ts
Mario Zechner e9c0ff4d84 feat(chord): Chord v2 is the package's root
src/next is src (the main export), src/next-node is src/node with short subpaths (/http, /workers, /plugins,
/plugins/runtime, /app, /surfaces, /web, /react, /tui, /tui/launcher); the foundations get /json and /watch;
Chord v1 moves to src/old (/old, /old/node) until p2 runs on v2; tests and docs move likewise (test/, docs/,
test/old, docs/old). Durable and env import the foundations from their subpaths.
2026-11-02 22:36:55 +00:00

16 lines
550 B
TypeScript

// The echo plugin: a WebSocket of its own protocol at `/echo/:room`, after the server checked who is connecting.
import { defineFacet } from "@earendil-works/chord";
import { Http } from "@earendil-works/chord/http";
export default defineFacet((_host, self) =>
self.contribute(Http, {
sockets: {
"/echo/:room": (socket, request, ctx) => {
const who = JSON.stringify(ctx.principal);
socket.send(`welcome to ${request.params.room}, ${who}`);
socket.on("message", (data) => socket.send(`echo: ${String(data)}`));
},
},
}),
);