0

I'm creating a test application to know a little more about websocket, in this application I create a chat system between client and user.

I'm using:

  • SQLServer;
  • Nestjs;
  • Socket.io;
  • React;

In my backend I configured a websocket Adapter to keep the default settings for all Gateways

import { IoAdapter } from '@nestjs/platform-socket.io';
import { ServerOptions, Socket } from 'socket.io';

export class SocketAdapter extends IoAdapter {
    createIOServer(
        port: number,
        options?: ServerOptions & {
            namespace?: string;
            server?: any;
        },
    ) {
        const server = super.createIOServer(port, {
            ...options,
            cors: {
                origin: '*',
            },
            pingInterval: 1000,
            pingTimeout: 10000,
            serveClient: true,
        } as ServerOptions);
        return server;
    }
}

On my front, socket.io is configured as follows:

import socketIOClient from "socket.io-client";

export const socket = socketIOClient(import.meta.env.VITE_APP_URL_SOCKETIO, {
    auth: {
        token: ""
    },
    transports: ['websocket', 'polling'],
    forceNew: true,
    reconnection: true,
    reconnectionDelay: 1000,
    reconnectionDelayMax: 5000,
    reconnectionAttempts: 5
});

I got to test several settings for pingInterval, pingTimeout, reconnectionDelay, forceNew but they all return the same problem.

The problem that occurs is, when the client is making a connection with the server, if for some reason the internet connection has fluctuated (remove the cable) and then returns, the SERVER should not disconnect the socket from the client immediately, thus having a waiting time for its reconnection.

Starting the server on my computer, I could see that the reconnection time works normally enter image description here

However, when sending the server application to an instance on AWS, Heroku or railway, none of them maintain the connection so that the socket can reconnect, when the connection goes offline, the server immediately shows the disconnected socket.

Because of this I thought it would be some wrong configuration in AWS with Load Balancer, but using heroku the same thing happened enter image description here

I tried following some settings I found, but none worked either.

What can be done so that the connection remains in the socket for a short time without disconnecting?

0 Answers0