@furystack/websocket-api

HTTP Api FuryStack package

Stats

StarsIssuesVersionUpdatedCreatedSize
@furystack/websocket-api
327.0.6015 days ago3 years agoMinified + gzip package size for @furystack/websocket-api in KB

Readme

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) {}
}

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.