1.0.3 • Published 5 years ago
@zfolk/adonis-wsex v1.0.3
adonis-wsex
Adonis provider for websocket check/exchange with authorized users
Requires
- Adonis framework v4.1
- Installed Adonis WebSocket Provider
- Installed Adonis Auth Provider
- Registered Auth middleware for WebSocket connections
- Allow connection only for authenticated users (guest users will be skipped)
Installation
npm i -S @zfolk/adonis-wsex
Setup
Add as provider (start/app.js)
const providers = [ '@zfolk/adonis-wsex/providers/WsExProvider' ]
Add socket channel (start/socket.js). Clients should subscribe to 'wsex'.
const WsEx = use('WsEx')
WsEx.channel().middleware('auth') // Adds channel/topic named 'wsex'
## Usage
> Broadcast to all connected users
```javascript
const WsEx = use('WsEx')
const event = 'message' // any string-named event
const data = { text: 'Text' } // any data|type serializable to json
WsEx.broadcast(event, data)
Emit to certain user by its id
const WsEx = use('WsEx') const userId = 23 // if user is offline - nothing will be done const event = 'message' // any string-named event const data = { text: 'Text' } // any data|type serializable to json
WsEx.emit(userId, event, data)
> Get online user ids
```javascript
const WsEx = use('WsEx')
const userIds = WsEx.getOnline() // returns <Array> with ids
Check if user is online
const WsEx = use('WsEx') const userId = 23 // user id needs to be checked
const online = WsEx.isOnline(userId) // returns , true === online