socket io disconnect some event work but not the others

19 views Asked by At

I'm developing a real-time chat service using NestJS and socket.io. It operates smoothly in a local environment, but when deployed with Kubernetes, GCP, load balancers, and Docker, I encounter either short or long cycles of disconnection depending on the configuration. The issue arises after a disconnection, as messages are still being stored in the database. Here's what happens:

After an initial disconnection, messages of connections and disconnections are repeatedly logged in the console. It seems to function this way due to socket.io's built-in features, but there's a part that I don't understand. If the connection is lost, message transmission should be impossible, yet messages are still being stored in Redis. However, the functionality to render these messages to the client does not work. Moreover, events designed to send alerts to the client are operating correctly. When the 'send' button is clicked by the client, the message is successfully sent to the server, and then the server triggers an event to send an alert back to the client, which also executes and displays successfully.

Therefore, features for preventing spam and filtering prohibited words are working, but when a user inputs a normal message, it does not appear on the screen.

 @UseGuards(WsGuard)
    @SubscribeMessage('new_message')
    async createChat(client: Socket, [value, channelId]: [value: string, channelId: string]) {
        const { userId, nickname } = client.handshake.auth.user;
        const filterWord = await searchProhibitedWords(value);
        Logger.log('클라이언트 아이디 확인확인', client.id); //콘솔에 찍힘
        Logger.log('채널아이디 확인확인', channelId); //콘솔에 찍힘
        if (filterWord) {
            return this.server.to(client.id).emit('alert', '허용하지 않는 단어입니다.'); //소켓 서버 끊겨도 알러트 뜨는거 확인.
        }

        const saveChat = await this.chatService.createChat(client, value, channelId, userId, nickname);
        Logger.log('세이브쳇 saveChat', saveChat); //콘솔에 찍힘
        if (saveChat === 'sameChat') {
            Logger.log('같은내용임 ');
            return this.server.to(client.id).emit('alert', '동일한 내용의 채팅입니다. 잠시 후 다시 시도해 주세요.');
        }
        if (saveChat === 'toFastChat') {
            Logger.log('빨라유 ');
            return this.server.to(client.id).emit('alert', '메세지를 전송할 수 없습니다. 메세지를 너무 빨리 보냈습니다.'); //연결 끊겨도 나오는거 확인.
        }
        Logger.log('new_message 실행중'); //콘솔에 찍힘

        const sendingMessage = this.server.to(channelId).emit('sending_message', value, nickname); //this cord dose not work!!
        Logger.log('sendingMessage', sendingMessage); //sendingMessage, true
        return sendingMessage;
    }

Why does it operate this way, where some functions work despite the connection being lost, while others do not? I'm very curious about this aspect. Is there anyone who can help?

0

There are 0 answers