wma

Websocket MessagePack API framework

Usage no npm install needed!

<script type="module">
  import wma from 'https://cdn.skypack.dev/wma';
</script>

README

Websocket Messagepack API framework

This is a work in progress.

A Nodejs, MessagePack, Websocket api framework

Install

npm install wma --save

Usage

Follow the comments in the code below.

// Import WMA and messagepack to get started

let wma = require('./../dist/bundle').wma;
let mp = require('msgpack-lite');

// Create a new server and add a handler

var app = new wma({
    port: 8642
});

let pingHandler = function(app, payload, server) {
    // app		:: gives access to the new app 
    // payload	:: Access to payload (if any)
    // server	:: access to the 'ws' instance of the particular connection
    //			   for which this handler is being executed.
    console.log('Handler is executed. Sending back a message.')
    let message = { mess: 'Server running at :: localhost:' + app.port };
    server.send(mp.encode(message));
};

let rcheckerHandler = function(app, payload, server) {
    // Run another handler from one handler
    console.log('Path is : ', this.path);
    app.runHandler('ping', payload, server);
}

app
    .handler({
        path: 'ping',
        handler: pingHandler
    })
    .handler({
        path: 'rhchecker',
        handler: rcheckerHandler
    });

// Implement a client for testing

var WebSocket = require('streamws');
var ws = new WebSocket('ws://127.0.0.1:8642/');

// Define what to do on receiving a message, before anything.

ws.on('message', (data, flags) => {
    console.log("Received a message : ", mp.decode(data));
});

// Execute the ping handler

ws.on('open', () => {
    ws.send(mp.encode({ path: 'rhchecker' }), { binary: true, mask: false });
});

Todo

  • Investigate conversion into UMD using Rollup
  • Create wma.runHandler to run other handlers from inside a handler.
  • Set up handler permissions (Handler X can/can't be executed from Handler Y)
  • Figure a way to provide API key creation/management functionality