New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@nestjs/platform-ws

Package Overview
Dependencies
Maintainers
4
Versions
269
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/platform-ws

Nest - modern, fast, powerful node.js web framework (@platform-ws)

  • 6.10.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
139K
increased by9.42%
Maintainers
4
Weekly downloads
 
Created

What is @nestjs/platform-ws?

@nestjs/platform-ws is a package that provides WebSocket support for NestJS applications using the ws library. It allows developers to create WebSocket gateways and handle real-time communication in a structured and scalable way.

What are @nestjs/platform-ws's main functionalities?

WebSocket Gateway

This feature allows you to create a WebSocket gateway using decorators. The `@WebSocketGateway()` decorator marks the class as a WebSocket gateway, and `@WebSocketServer()` injects the WebSocket server instance. The `@SubscribeMessage()` decorator listens for specific messages, and the `handleMessage` method processes incoming messages.

import { WebSocketGateway, WebSocketServer, SubscribeMessage, MessageBody } from '@nestjs/websockets';
import { Server } from 'ws';

@WebSocketGateway()
export class EventsGateway {
  @WebSocketServer()
  server: Server;

  @SubscribeMessage('message')
  handleMessage(@MessageBody() data: string): string {
    return data;
  }
}

Handling WebSocket Events

This feature demonstrates how to handle WebSocket lifecycle events such as initialization, connection, and disconnection. Implementing `OnGatewayInit`, `OnGatewayConnection`, and `OnGatewayDisconnect` interfaces allows you to define methods that are triggered during these events.

import { WebSocketGateway, WebSocketServer, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
import { Server, Socket } from 'ws';

@WebSocketGateway()
export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
  @WebSocketServer()
  server: Server;

  afterInit(server: Server) {
    console.log('WebSocket server initialized');
  }

  handleConnection(client: Socket, ...args: any[]) {
    console.log('Client connected:', client.id);
  }

  handleDisconnect(client: Socket) {
    console.log('Client disconnected:', client.id);
  }
}

Other packages similar to @nestjs/platform-ws

FAQs

Package last updated on 05 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc