How to create socket over https in NestJs?

Viewed 449

I have created one socket gateway which is working very smoothly with an HTTP request. Now, I am trying to connect socket through https request in NestJs but didn't work for me. I have also tried to give extra parameters in @WebsocketGateway(5058, { origin : "*:*", secure: true })

I have also checked for NestJs official documentation to work with SSL on the socket but found nothing.

Below is my code which I have created as per documentation.

import { InternalServerErrorException, BadRequestException } from '@nestjs/common';
import { SocketService } from './socket/socket.service';
import { Server, Socket } from 'socket.io';

@WebSocketGateway(5058, { origin : "*:*"} )
export class AppGateway implements OnGatewayConnection, OnGatewayInit {

    constructor(private socketService: SocketService) { }

    public userIds = [];

    afterInit(server: Server) {
        console.log("Socket server started");
        this.socketService.socket = server;
    }

    async handleConnection(client) {

        try {
            console.log(client.id);
            this.socketService.socket.to(client.id).emit('status', "connected = " + client.id);
        } catch (error) {
            throw new InternalServerErrorException(
                `Oops.something went wrong, please try again later`,
            );
        }
    }

    async handleDisconnect(client) {
        this.userIds = this.userIds.filter(user => user.conn_socket_id !== client.id);
    }
}

edited:

I can start server and access socket while using an HTTP request, but I am not able to access the socket on HTTPS request. ex. http://example.com:5058 is working for me, https://example.com:5058 is not working.

0 Answers
Related