2.1.1 • Published 2 years ago

@superhero/core.websocket v2.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

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.

2.0.3

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

0.2.0

6 years ago

0.1.4

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago