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.
npm i -D wsmanager
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?');
})
...
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
MIT © Kostas Bariotis