Skip to content
Meirion Hughes edited this page Jul 14, 2017 · 2 revisions

Introduction

viae is a middleware framework, like express, hapi and koa. The primary difference with viae is that the communication medium is abstracted; also, both the client and server can have APIs. i.e. Servers can make requests to clients.

Wire | WireServer

A wire connection is an interface of the form:

interface Wire {
  sid?: string;
  address?: string;

  send(data: ArrayBuffer): void;
  close();

  on(event: "message", cb: (data: ArrayBuffer) => void): void;
  on(event: "close", cb: () => void): void;
  on(event: "error", cb: (err: any) => void): void;

  [index: string]: any;
}

apart from the (optional) sid and address fields, this interface matches that of the standard WebSocket class.

the WireServer interface has the form:

interface WireServer {
  on(event: "connection", cb: (connection: Wire) => void);
}

Which is also implemented by the ws npm module by default. You don't have to use WebSockets and if you are so inclined you can implement these interfaces using a different communication medium.

Implementation of the Session ID (sid) and the Remote Address is optional. Neither are implemented by default by WebSocket.

Via

The main workhorse is the Via class, which directs the sending and receiving of messages.

import {Via} from 'viae';
let via = new Via(/*[optional] wire */);

Middleware

When a message package is received from the wire the following sequence occurs:

  • Deserialise and Create a Context;
  • Process the Context through
    • the before middleware, then
    • the streamer middleware, then
    • the interceptor middleware, then
    • the app middleware, then
    • the unhandled middleware.
  • Clear the _done flag
  • Process the Context through the after middleware.

Before

the before middleware allows you to examine the context immediately after deserialisation and before any handling of the context. By default it is empty and you can add handlers to it like so:

via.before((ctx: ViaContext) => {
});

returning false here will actually terminate any further general processing on the context - it will be processed by the after middleware.

Interceptor

App

Unhandled

After

Clone this wiki locally