Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nestjs/websockets

Package Overview
Dependencies
Maintainers
1
Versions
331
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/websockets

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

  • 5.0.0-beta.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
433K
decreased by-13.37%
Maintainers
1
Weekly downloads
 
Created

What is @nestjs/websockets?

@nestjs/websockets is a module for the NestJS framework that provides WebSocket capabilities. It allows developers to create real-time, event-driven applications using WebSockets, which are essential for applications requiring instant data updates, such as chat applications, live notifications, and online gaming.

What are @nestjs/websockets's main functionalities?

WebSocket Gateway

This feature allows you to create a WebSocket gateway that listens for incoming WebSocket connections and messages. The `@WebSocketGateway` decorator is used to define a WebSocket gateway, and the `@SubscribeMessage` decorator is used to handle specific message events.

```typescript
import { WebSocketGateway, WebSocketServer, SubscribeMessage, MessageBody } from '@nestjs/websockets';
import { Server } from 'socket.io';

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

  @SubscribeMessage('message')
  handleMessage(@MessageBody() message: string): void {
    this.server.emit('message', message);
  }
}
```

WebSocket Client

This feature allows you to create a WebSocket client that can connect to a WebSocket server. The `@WebSocketClient` decorator is used to define a WebSocket client, and the `OnGatewayInit`, `OnGatewayConnection`, and `OnGatewayDisconnect` interfaces are used to handle client lifecycle events.

```typescript
import { Injectable } from '@nestjs/common';
import { WebSocketClient, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
import { Socket } from 'socket.io-client';

@Injectable()
@WebSocketClient()
export class ChatClient implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
  private client: Socket;

  afterInit(client: Socket) {
    this.client = client;
    console.log('WebSocket client initialized');
  }

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

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

Custom WebSocket Server

This feature allows you to create a custom WebSocket server using the `ws` library. The `@WebSocketGateway` decorator can accept a custom server instance, and you can handle connection and message events directly.

```typescript
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'ws';

@WebSocketGateway({ server: new Server({ port: 8080 }) })
export class CustomGateway {
  @WebSocketServer()
  server: Server;

  constructor() {
    this.server.on('connection', (socket) => {
      socket.on('message', (message) => {
        console.log('Received message:', message);
      });
    });
  }
}
```

Other packages similar to @nestjs/websockets

FAQs

Package last updated on 22 Apr 2018

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