fictorial-base-wss

1.0.0 • Public • Published

A generic JSON-over-WebSockets server.

Basic Usage

Using defaults:

const {server, clients} = require('fictorial-base-wss')((socket, msg) => {
  ...
})

Or being explicit:

const {server, clients} = require('fictorial-base-wss')({
  port: 8000,

  maxMissedPongs: 3,
  pingEveryMillis: 29000,

  logger: new (winston.Logger)({
    transports: [
      new (winston.transports.Console)(),
      new (winston.transports.File)({ filename: 'somefile.log' })
    ]
  }),

  onMessage: function (socket, msg) {
    // ...
  },

  onDisconnect: function (socket) {
    // ...
  }
})

Sending Data

The socket is a WebSocket from the ws module. Thus, go ahead and send it something in your handlers. socket.send(JSON.stringify({...}))

Error Handling

If the handler throws an error that has a .fatal or .close property set to true, the client WebSocket connection will be disconnected.

Client Identity

If the handler wishes to identify clients by some name, it should set .clientId to that name. Then, connected clients may be found via clients[someId].

Readme

Keywords

Package Sidebar

Install

npm i fictorial-base-wss

Weekly Downloads

0

Version

1.0.0

License

MIT

Last publish

Collaborators

  • fictorial