@sco-techlab/nestjs-websockets
TypeScript icon, indicating that this package has built-in type declarations

10.0.3 • Public • Published

plot

Nest.JS i18n

Nest.JS Websockets is a Websocket gateway module (Clients, events) management for Nest.JS framework.

Get Started

  • Install dependency
npm i @sco-techlab/nestjs-websockets && npm i @nestjs/platform-socket.io
  • Import Websocket module in your 'app.module.ts' file, register or registerAsync methods availables
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { WebsocketModule } from '@app/nestjs-websockets';

@Module({
  imports: [

    // Import WebsocketModule without register, you must import it on any other module to use it on the module
    //WebsocketModule,

    // Import WebsocketMoudle with register method to use it globally in the application
    WebsocketModule.register(),
  ],
  providers: [AppService],
})

export class AppModule {}
  • You can import the module in global mode, to use trasnalte service only need constructor dependency inyection
  • Add your Websocket Adapter to your application in 'main.ts' file
import { NestFactory } from '@nestjs/core';
import { Logger, INestApplication } from '@nestjs/common';
import { WebsocketAdapter } from '@app/nestjs-websockets';
import { AppModule } from './app.module';

async function bootstrap() {

  const app: INestApplication = await NestFactory.create(AppModule, 
    { 
      logger: new Logger(),
    }
  );

  app.useWebSocketAdapter(new WebsocketAdapter(app));

  await app.listen(3005);
  console.log(`[App] App started in 'http://localhost:3005'`);
}
bootstrap();

Service example

import { Injectable } from "@nestjs/common";
import { SubscribeEvent, WebsocketService } from "@app/nestjs-websockets";

@Injectable()
export class AppService {

  // Inject the WebsocketService to access the server and clients
  constructor(private readonly websocketService: WebsocketService) {

    // Subscribe to the 'handleConnection' event to get the client sockets
    this.websocketService.handleConnection().subscribe((socket) => {
      console.log(`[handleConnection] Client connected:`, socket['handshake'].headers.origin, socket.id);

      // Subscribe to the 'event_init' event to get the client sockets
      this.websocketService.client(socket.id).on('event_init', (data: any[]) => {
        console.log(`[handleConnection - onClient] Event received from client '${socket.id}':`, data);
      });
    });

    // Subscribe to the 'handleDisconnect' event to get the client sockets
    this.websocketService.handleDisconnect().subscribe((socket) => {
      console.log(`[handleDisconnect] Client disconnected:`, socket['handshake'].headers.origin);
    });
  }

  // Use decorator to subscribe a event emited by one client
  @SubscribeEvent('event_init')
  handleEventInit(event: string, client: string, args?: any[]): void {
    console.log(`[WebsocketGateway - handleEventInit] Event '${event}' received from client '${client}':`, args);
    return;
  }

  // Use decorator to subscribe on any event emited by any clients, example 1
  @SubscribeEvent()
  handleGlobalOne(event: string, client: string, args?: any[]): void {
    console.log(`[WebsocketGateway - handleGlobalOne] Event '${event}' dispatch on global subscription (1) from client '${client}':`, args);
    return;
  }

  // Use decorator to subscribe on any event emited by any clients, example 2
  @SubscribeEvent()
  handleGlobalTwo(event: string, client: string, args?: any[]): void {
    console.log(`[WebsocketGateway - handleGlobalTwo] Event '${event}' dispatch on global subscription (2) from client '${client}':`, args);
    return;
  }
}

Author

Santiago Comeras Oteo

plot

Package Sidebar

Install

npm i @sco-techlab/nestjs-websockets

Weekly Downloads

4

Version

10.0.3

License

none

Unpacked Size

30 kB

Total Files

26

Last publish

Collaborators

  • sco-techlab