0.3.0 • Published 4 years ago

beamed v0.3.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

beamed

Build Status

A blazing fast, slim communication protocol for NodeJS IPC.

NPM

Features

  • A lightweight protocol minimizes network traffic and provides high processing performance
  • The TypeScript API enables you to specify strictly typed endpoints and reuse the same type definitions on client and server side
  • Support for Unix, Windows, TCP and TLS sockets
  • Requesting, messaging and publish / subscribe
  • Send any payload (objects, buffers and strings)
  • No third party dependencies

Example

shared.ts

enum AuthTopics {
  login, // can be strings or numbers
}
interface AuthApi {
  [AuthTopics.login]: {
    req: Credentials;
    res: User;
  };
}

server.ts

import { createServer } from "beamed";
import { AuthApi, AuthTopics } from "./shared";

const bs = createServer<AuthApi>()
  .onRequest(AuthTopics.login, authenticate)
  .listen("/tmp/auth-test");

client.ts

import { connect } from "beamed";
import { AuthApi, AuthTopics } from "./shared";

const bc = connect<AuthApi>("/tmp/auth-test");
bc.request(AuthTopics.login, new Credentials("user", "p4ssw0rd")).then((user) =>
  console.log(user)
);

Protocol

Message types (Not completely implementing yet).

TypeMessage-PatternExamples
Request<length>?<topic>\|<id>[\|payload]19?1\|8\|J{"foo":"bar"}5?2\|45
Response<length>.<id>[\|payload]17.8\|J{"foo":"bar"}3.45
Error-Response<length>X<id>\|<error-code>[\|message]17.8\|J{"foo":"bar"}6X45\|42
Subscribe<length>+<topic>2+1
Unsubscribe<length>-<topic>2-1
Message / Push<length>!<topic>[\|payload]17!1\|J{"foo":"bar"}2!2

Where the tokens have the following format

TokenFormatNote
lengthnumericThe byte length of the message content without this length (auto-generated)
topicutf-8 (except \|)You can use TS Enums
idnumericThe request id (auto-generated)
error-codeutf-8 (except \|)You can use TS Enums
messageutf-8Custom Error decription
payloadutf-8 or BufferPrefixed by a character that determines the content type:J for Json,B for binary data,T for text