A bi-directional communication framework.
The goal in developing viae was to allow making asynchronous req/res on a single websocket connection and to allow sending objects containing TypedArrayView
instances. It evolved to facilitate sending and receiving nested streams. Its opinionated as to the default encoder (msgpack) but json and cbor can be used too.
A server is created by instantiating a Viae
instance and passing a WebSocketServer
instance to it:
let server = new WebSocketServer({ port: 8080, host: "0.0.0.0" });
let viae = new Viae(server);
request middleware can then be added using:
viae.use(async (ctx, next) => {
//do something with ctx
return next();
});
A controller-based router is available by using
@Controller('chat')
class ChatRoomController {
private _channel = new Subject<string>();
@Get()
join() {
return this._channel;
}
@Post()
addMsg(@Data() msg: string){
this._channel.next(msg);
return Status.OK;
}
}
viae.use(new App({
controllers: [new ChatRoomController()]
}));
a client is created by instantiating a Via
instance and making requests
let wire = new WebSocket("ws://0.0.0.0:8080");
let via = new Via({ wire: wire as any });
via.on("open", async () => {
let joinRes = await via.request("GET", "/chat");
if (isObservable(joinRes.data)) {
joinRes.data.forEach(x => console.log(x));
}
await via.request("POST", "/chat", "hello world...");
await new Promise((r, _) => setTimeout(r, 100));
wire.close();
});
npm install
npm test
"Cross" Icon courtesy of The Noun Project, by Alexander Skowalsky, under CC 3.0