1

I am trying to connect to my Nestjs websocket with postman for rapid testing during development but I am having a lot of trouble getting postman to actually connect.

This is my errror

Error: connect ECONNREFUSED 127.0.0.1:8000

Here is my server code:

import { Logger } from '@nestjs/common';
import { OnGatewayConnection, OnGatewayInit, SubscribeMessage, WebSocketGateway } from '@nestjs/websockets';

@WebSocketGateway({ namespace: "test", cors: true })
export class AppGateway implements OnGatewayInit, OnGatewayConnection {

  private logger: Logger

  constructor() {
    this.logger = new Logger('AppGateway')
  }

  afterInit(server: any) {
    this.logger.log("Gateway is running")
  }

  handleConnection(client: any, ...args: any[]) {
    this.logger.log("Client Connected")
  }

  @SubscribeMessage('message')
  handleMessage(client: any, payload: any): string {
    return 'Hello world!';
  }

  
}

This is extremely simplistic because I actually spun up a new nestjs server incase it was something to do with authentication

Here is a picture of my postman UI to show what I am doing there (I am using socket-io v4 on my server which is why I have that option selected on postman) enter image description here

I have also tried the url ws://127.0.0.1:8000/test but this produces the same error (also have tried using localhost instead of 127.0.0.1)

I know my websocket server is functioning correctly (and on the correct port) because I spun up a quick react app using the socketio client library and it connected to the websocket fine.

Here is the code to my quick react app:

import logo from './logo.svg';
import './App.css';
import { io } from 'socket.io-client'

function App() {

  const socket = io("http://localhost:8000/test")
  console.log(socket)
  
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

I have been stuck on this for a day or two now so any help would be great!

Thanks in advance.

Aaron56
  • 98
  • 2
  • 6

1 Answers1

1

I had a similar problem when running NestJS in WSL and trying to connect via Postman.

As far as I understood, there is a bug report that, when using socket.io feature, Postman attempts to convert localhost to 127.0.0.1 which does not work because it's not the WSL IP.

So, in order to make it work you need:

  1. Open a PowerShell to get WSL IP (it changes every time WSL reboots):
wsl hostname -I
  1. Use the output IP to connect via Postman:
http://<ip>:8000/test

About http or ws, using socket.io, it doesn't matter if you choose http or ws to connect, socket.io docs tells us the following

You can use either https or wss (respectively, http or ws).

Elton
  • 41
  • 3