@superhero/core.websocket

Addon module to the @superhero/core module, adds a websocket server option

Stats

stars šŸŒŸissues āš ļøupdated šŸ› created šŸ£size šŸ‹ļøā€ā™€ļø
00Sep 18, 2020May 13, 2018Minified + gzip package size for @superhero/core.websocket in KB

Readme

Core ā€ŗ Websocket

Licence: MIT


npm version

This is an addon module that implements the superhero/websocket module with the superhero/core module. This addon will apply an additional option to the server, eg: websocket. As websockets are bidirectional, a different multiplex dispatcher approach is required.

Have a look at the superhero/websocket module for more information, code and tests related to what's presented here.

Install

npm install @superhero/core.websocket

...or just set the dependency in your package.json file:

{
  "dependencies":
  {
    "@superhero/core.websocket": "*"
  }
}

Example Application

A simple example to get started follows.

Example Application ā€ŗ File structure

App
ā”œā”€ā”€ controller
ā”‚   ā”œā”€ā”€ foobar.js
ā”‚   ā””ā”€ā”€ logger.js
ā”œā”€ā”€ config.js
ā”œā”€ā”€ index.js
ā””ā”€ā”€ package.json

package.js

{
  "name": "Super Duper App",
  "version": "0.0.1",
  "description": "An example meant to describe the libraries fundamentals",
  "license": "MIT",
  "dependencies": {
    "@superhero/core.websocket": "*"
  }
}

config.js

module.exports =
{
  bootstrap:
  {
    '@superhero/core.websocket':{}
  },
  routes:
  [
    {
      endpoint  : 'controller/foobar',
      chain     : 'controller/logger',
      policy    : 'foo'
    }
  ]
}

index.js

const
config  = require('./config'),
core    = require('@superhero/core')

core.bootstrap(config.bootstrap).then(() => core.server('websocket', config.routes).listen(80))

controller/foobar.js

const Dispatcher = require('@superhero/core.websocket/dispatcher')

module.exports = class extends Dispatcher
{
  async dispatch()
  {
    await this.session.emit(this.input.event, this.input.data)
  }
}

controller/logger.js

const Dispatcher = require('@superhero/core.websocket/dispatcher')

// Middleware dispatcher
// Inherits the same interface and functionality as an endpoint dispatcher
module.exports = class extends Dispatcher
{
  async dispatch(next)
  {
    console.log('received', new Date().toISOString())

    await next()

    console.log('done', new Date().toISOString())
  }
}

The bootstrap process is the key to add and be able to use the addon.

You can now connect to the server and send the event "foo" with any data, and it will respond with that same event and data. This is ofc useless, but works as an example to show some fundamentals about the module.

If you want to read more a more about the client end, have a look at the dependent module: superhero/websocket that also contain a browser version to connect to the socket through.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.