Skip to content

kbariotis/wsmanager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

An easy to use WebSocket account manager

Resources

License

Stars

Watchers

Forks

Packages

No packages published