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)
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.