wsmanager

1.1.0 • Public • Published

wsmanager

NPM version Build Status Coveralls Status Dependency Status Downloads

An easy to use WebSocket manager.

WSManager keeps an in memory hashmap with all active WS connections and maintains it through out app's lifecycle. WSManager stores different accounts and their connected clients as WebSocket objects that you can retrieve at any time.

Install

npm i -D wsmanager

Usage

I am using ws

...
 
import wsmanager from "wsmanager"
 
const account = 'SOME_ACCOUNT';
 
wss.on('connection', function (ws) {
 
    wsmanager.connect(account, ws);
    
    ws.on('close', function() {
        wsmanager.disconnect(account, ws);    
    })
})
 
...
 
let connections = wsmanager.getConnectionsFrom(account)
connections.forEach(function(ws){
    ws.send('Sup?');
})
 
...
 

Docs

Add a client to that Account, using a function directly:

wsmanager.connect(account, socket)
  • account(required): a unique identifier for an account
  • socket(required): a WebSocket object

or using events:

wsmanager.emit('connection', {account:account, socket:socket})

Remove a client, using a function directly:

wsmanager.disconnect(account, socket)
  • account(required): a unique identifier for an account
  • socket(required): a WebSocket object

or using events:

wsmanager.emit('disconnection', {account:account, socket:socket})

Get all connections:

wsmanager.getConnections()

Get all connections by an Account:

wsmanager.getConnections(account)
  • account(required): a unique identifier for an account

License

MIT © Kostas Bariotis

Readme

Keywords

Package Sidebar

Install

npm i wsmanager

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators

  • kbariotis