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

@nestjs/platform-socket.io

Package Overview
Dependencies
Maintainers
0
Versions
257
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/platform-socket.io

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

  • 10.4.15
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
317K
decreased by-28.5%
Maintainers
0
Weekly downloads
 
Created

What is @nestjs/platform-socket.io?

@nestjs/platform-socket.io is a package that integrates Socket.IO with the NestJS framework, allowing for real-time, event-based communication between the server and clients. It leverages the modular architecture of NestJS to provide a structured and scalable way to handle WebSocket connections.

What are @nestjs/platform-socket.io's main functionalities?

Basic WebSocket Gateway

This feature allows you to create a basic WebSocket gateway using the @nestjs/websockets module. The `@WebSocketGateway` decorator marks the class as a WebSocket gateway, and the `@WebSocketServer` decorator injects the Socket.IO server instance. The `@SubscribeMessage` decorator listens for incoming messages and handles them accordingly.

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

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

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

Namespace Support

This feature allows you to create WebSocket gateways with specific namespaces. The `namespace` option in the `@WebSocketGateway` decorator specifies the namespace for the gateway, enabling you to organize your WebSocket events more effectively.

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

@WebSocketGateway({ namespace: 'chat' })
export class ChatGateway {
  @WebSocketServer()
  server: Server;

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

Room Support

This feature allows you to manage rooms within your WebSocket server. Clients can join specific rooms, and messages can be broadcasted to all clients within a room. The `client.join(room)` method adds a client to a room, and `this.server.to(room).emit('message', message)` sends a message to all clients in the specified room.

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

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

  @SubscribeMessage('joinRoom')
  handleJoinRoom(@MessageBody() room: string, @ConnectedSocket() client: Socket): void {
    client.join(room);
  }

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

Other packages similar to @nestjs/platform-socket.io

FAQs

Package last updated on 09 Dec 2024

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