Options
All
  • Public
  • Public/Protected
  • All
Menu

@jacekpietal/bouncer.js

@jacekpietal/bouncer.js

(ノಠ益ಠ)ノ <-- bouncer is a person letting you inside a club

npm package version downloads total tests status

web-sockets micro-service manager and static files server at the same port

  • using express (and socket.io)
  • or uWebSockets.js

Install

Express + socket.io variant

$ yarn add @jacekpietal/bouncer.js express [socket.io]

socket.io only if you plan on using websockets

MicroWebSockets variant

$ yarn add @jacekpietal/bouncer.js uWebSockets.js@github:uNetworking/uWebSockets.js#v20.6.0

Running

$ [PORT=4200] yarn bouncer.js folder [--express] [--uws] [--debug] [--plugin path/to/file.js]
  • PORT=4200 - choose server port
  • folder - folder you want to server static files from
  • --express - optional parameter to start express + socket.io variant
  • --uws - optional parameter to start microwebsockets variant
  • --debug - show logs
  • --plugin path/to/file.js - path to WSPlugin, can use multiple times

Demos

https://github.com/Prozi/bouncer.js/tree/master/demo

$ git clone https://github.com/Prozi/bouncer.js
$ cd bouncer.js
$ yarn

Express + socket.io variant

$ yarn add express socket.io
$ yarn express

MicroWebSockets variant

$ yarn add uWebSockets.js@github:uNetworking/uWebSockets.js#v20.6.0
$ yarn uws

then visit http://localhost:4200

Flow

STEP 1: Before Connection

  • client -> connects websocket to bouncer server on websocket protocol
  • server -> waits for join event (which is defined in config.join)

STEP 2: Connection

  • client -> sends join event with room name (topic/plugin name)
  • server -> if such plugin is configured joins client to that plugin
  • server -> broadcasts to all of that room that client has joined

STEP 3: After Connection

  • client -> does some actions (emits, receives)
  • server -> plugin responds to websocket actions

STEP 4: Finish Connection

  • client -> disconnects after some time
  • server -> broadcasts to all people from room that client left (config.leave)

Plugins

{ "event": "/join", "data": "pluginName" }
  • a plugin is a function (ws, { id, event, data }) that is called each time the frontend websocket emits to server
  • context (this) of each plugin is the bouncer instance.
  • plugins receive (and send) the data in the format of:
{
id, // WebSocket id - this is automatically added
event, // event name as string
data, // any data accompanying the event
}

License

MIT

Generated using TypeDoc