JSPM

@sco-techlab/nestjs-websockets

10.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q33389F

Websocket (Clients, events) management for Nest.JS framework

Package Exports

  • @sco-techlab/nestjs-websockets
  • @sco-techlab/nestjs-websockets/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@sco-techlab/nestjs-websockets) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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