Nondeterministic Postrequisite Metaprotocol

    @furystack/websocket-api
    TypeScript icon, indicating that this package has built-in type declarations

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

    Install

    npm i @furystack/websocket-api

    DownloadsWeekly Downloads

    8

    Version

    8.0.4

    License

    GPL-2.0

    Unpacked Size

    86.2 kB

    Total Files

    69

    Last publish

    Collaborators

    • gallayl