wsbasic

1.0.51 • Public • Published

wsBasic

wsBasic is a simple hook-on utility for ws.

Examples:

Server:

const WebSocket = require("ws"); /// require ws
const wss = new WebSocket.Server({ port: 8080, clientTracking: true }); /// define ws server
require("wsbasic")(wss); /// enable wsbasic

On connection:

/// gives a uuid for each client
wss.on('connection', function connection(ws) {
  console.log(ws.uuid); /// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
});

To specific UUID:

wss.on('connection', function connection(ws) {
  wss.sendToUUID('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'hello');
});

To client with specific attribute:

wss.on('connection', function connection(ws) {
  wss.sendToAttribute({"channel": "/"}, 'hello');
});

Channels:

wss.on('connection', function connection(ws) {
  ws.channel = "hello";
  wss.broadcast("hello", "hi"); // as you probably guessed, this will send a message to all clients in "hello"
});

Readme

Keywords

Package Sidebar

Install

npm i wsbasic

Weekly Downloads

0

Version

1.0.51

License

ISC

Unpacked Size

22.2 kB

Total Files

4

Last publish

Collaborators

  • drakexyz