viae
TypeScript icon, indicating that this package has built-in type declarations

1.4.1 • Public • Published

Viae

A bi-directional communication framework.

logopng

NPM version NPM downloads Travis Status codecov Stability

⚠️ Currently still under development. ⚠️

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 rxjs Observables (stream request, stream response);

Basic Usage

A server is created by instantiating a Viae instance and passing a WebSocketServer (or an alterative IWireServer), 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 with a wire and making requests

let wire = new WebSocket("ws://0.0.0.0:8080");
let via = new Via({ wire });

via.on("open", async () => {

  let data = await via.call("GET", "/chat");

  data.forEach(x => console.log(x));
  
  await via.call("POST", "/chat", "hello world...");

  await new Promise((r, _) => setTimeout(r, 100));

  wire.close();
});

Build

npm install
npm test

Credits

"Cross" Icon courtesy of The Noun Project, by Alexander Skowalsky, under CC 3.0

Package Sidebar

Install

npm i viae

Weekly Downloads

21

Version

1.4.1

License

MIT

Unpacked Size

126 kB

Total Files

78

Last publish

Collaborators

  • meirionhughes