Client supporting WebSockets via yaschema-ws-api
export const stream = makeWsApi({
routeType: 'stream',
url: '/stream',
schemas: {
requests: {
ping: schema.object({ echo: schema.string().allowEmptyString().optional() }).optional()
},
responses: {
pong: schema.object({
body: schema.string()
})
}
}
});
const connection = await apiWs(
stream,
{},
{
pong: async ({ input }) => {
console.log(`GOT ${input.body}`)
}
}
);
await connection.output.ping({ echo: 'Hello World!' });
The above example demonstrates basic use of a yaschema-ws-api-client, which lets you define runtime and compile-time types for bidirectionally working with WebSockets. See the express-yaschema-ws-api-handler package for adding handlers to Express.
This library supports web-based WebSockets by default and supports the Node ws package by calling:
setWebSocket(WebSocket as any as CommonWebSocket);
While the Node ws WebSocket interface doesn't perfectly align with the web equivalent, for the aspects we use, it does.
Thanks for checking it out. Feel free to create issues or otherwise provide feedback.
Be sure to check out our other TypeScript OSS projects as well.