Samuel A. Souza
Samuel A. Souza

Reputation: 377

How can I test a NestJS WebSocket Server with Insomnia? Error: "socket hang up"

I'm trying to test my WebSocket Server. I want to connect to the server and verify If I can log an access token in the terminal. The Insomnia:

enter image description here

My Nest implementation:

@WebSocketGateway(0, {
  cors: {
    origin: '*',
  },
})
export class WorkGateway {
  @WebSocketServer()
  private server: Server;
  private logger: Logger = new Logger('WorkSocket');

  constructor(private readonly authService: AuthService) {}

  handleConnection(client: Socket) {
    const id = this.authService.verify({
      accessToken: client.handshake.headers.authorization,
    });

    console.log(id);
  }
}

From the React client, I'm able to connect, but not from Insomnia.

Upvotes: 0

Views: 3255

Answers (2)

M C
M C

Reputation: 256

There is no support for socket io on Insomnia at current time. Go for Postman which support it since 2021.

Upvotes: 1

capraljohn
capraljohn

Reputation: 564

Insomnia does not support Socket.io connection directly, only WebSocket. I tried to find a solution to the problem, how to test complex websocket connections with multiple event subscriptions, but it seems that so far only a simple connection can be configured, which is probably the solution for you. Try using - ws://localhost:3333/socket.io/?EIO=4&transport=websocket

Instead of - ws://localhost:3333/

Also try changing the configuration to

@WebSocketGateway({ cors: { origin: '*', }, transports: ['websocket'], })

Upvotes: 2

Related Questions