Skip to content

Socket disconnects on big payload. #5140

Open
@faisal-anwar825

Description

@faisal-anwar825

I am using socket.io to create a realtime chat which includes attachments as well. I am sending files in Base64 form. but the issue is that if file size is around 500 to 600 KB. it gets send successfully but when file size increases more than that the sockets disconnects as soon as i send the message, with the error

Transport close: The connection was closed

i have tried increasing the maxHttpBufferSize to 1MB and 100MB but still files > 7, 800 KBs wont upload.

Socket.IO server version: 4.7.2

This is the code from my server

`

    import {
      WebSocketGateway,
      WebSocketServer,
      SubscribeMessage,
      OnGatewayConnection,
      OnGatewayDisconnect,
    } from '@nestjs/websockets';
    import { Server, Socket } from 'socket.io';
    import { SocketsService } from '../sockets.service';
    import { WsGuard } from '@app/common';
    import { ChatDto } from '../dto/chat.dto';
    import { Injectable, UseGuards } from '@nestjs/common';
    
    @Injectable()
    @WebSocketGateway({ cors: '*:*' })
    export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
      @WebSocketServer() public server: Server;
      users = 0;
    
      constructor(
        private readonly socketService: SocketsService,
      ) { }
    
      async onModuleInit() {
        // Call the configureWebSocketServer method when the module has been initialized
        this.configureWebSocketServer();
      }
    
      async handleConnection(client: Socket) {
        // A client has connected
        this.users++;
    
        client.removeAllListeners('disconnect');
    
        client.on('disconnect', () => this.handleDisconnect(client));
        // Notify connected clients of current users
        this.server.emit('users', this.users);
      }
    
      afterInit(server: Server) {
        this.socketService.socket = server;
      }
    
      async handleDisconnect(client: Socket) {
        // A client has disconnected
        this.users--;
        client.removeAllListeners('disconnect');
    
        client.on('disconnect', () => this.handleDisconnect(client));
        // Notify connected clients of current users
        this.server.emit('users', this.users);
      }
    
      @SubscribeMessage('chat')
      @UseGuards(WsGuard)
      async onChat(client: Socket, payload: ChatDto) {
        // expect to receive the file here
      }
    
      async configureWebSocketServer() {
    
        this.server.engine.opts.maxHttpBufferSize = 4e8;
      }
    }

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions