@furystack/websocket-api

12.0.2 • Public • Published

websocket-api

WebSocket implementation for FuryStack

Usage example

You can initialize the WebSocket package in the following way

const myInjector = new Injector()
  .useWebsockets({
    path: '/api/sockets',
    actions: [WhoAmI],
  })

Implement your own actions

You can implement a WebSocket action in the following way:

import { User } from '@furystack/core'
import { HttpUserContext } from '@furystack/http-api'
import { Injectable } from '@furystack/inject'
import { Data } from 'ws'
import * as ws from 'ws'
import { IWebSocketAction } from '../models/IWebSocketAction'

@Injectable({ lifetime: 'transient' })
export class WhoAmI implements WebSocketAction {
  public dispose() {
    /** */
  }
  public static canExecute(data: Data): boolean {
    return data.toString() === 'whoami' || data.toString() === 'whoami /claims'
  }

  public async execute() {
    const currentUser = await this.httpUserContext.getCurrentUser()
    this.websocket.send(JSON.stringify(currentUser))
  }

  constructor(private httpUserContext: HttpUserContext<User>, private websocket: ws) {}
}

Readme

Keywords

Package Sidebar

Install

npm i @furystack/websocket-api

Weekly Downloads

38

Version

12.0.2

License

GPL-2.0

Unpacked Size

85.9 kB

Total Files

59

Last publish

Collaborators

  • gallayl