0.1.0 • Published 4 years ago

nestjs-ws v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

nestjs-ws

NPM version

A ws client service for NestJS.

Installation

$ npm install nestjs-ws --save

Getting Started

To use the WsService, first import WsModule.

import { Module } from '@nestjs/common';
import { WsModule } from 'nestjs-ws';

@Module({
  imports: [
    WsModule.register({ url: 'ws://www.host.com/path' }),
  ],
})
export class AppModule {}

Next, inject WsService using normal constructor injection.

import { Injectable } from '@nestjs/common';
import { WsService } from 'nestjs-ws';

@Injectable()
export class TestService {
  constructor(private readonly wsService: WsService) {}

  async root(): Promise<boolean> {
    const ws = await this.wsService.getClient();
    return true;
  }
}

Configuration

Single Client

@Module({
  imports: [
    WsModule.register({ url: 'ws://www.host.com/path' }),
  ],
})

Multi Clients

@Module({
  imports: [
    WsModule.register([
      { name: 'ws1', url: 'ws://www.host1.com/path' },
      { name: 'ws2', url: 'ws://www.host2.com/path' },
    ]),
  ],
})

Async configuration

@Module({
  imports: [
    WsModule.registerAsync({
      useFactory: (configService: ConfigService) => configService.get('ws'),
      inject:[ConfigService]
    }),
  ],
})

License

MIT