Search code examples
reactjsnext.jssocket.ionestjsnestjs-gateways

How to get nestjs websocket exception error in client side using react?


I am try to create a realtime project for practice. I use nestjs for backend and react for frontend. Here in nestjs I am suing websocket.

In nestjs I write this code

@SubscribeMessage("createMessage")
handleCreate(
    @MessageBody() messageInput: MessageInput
): WsResponse<string> {
    console.log(messageInput)
    if (messageInput.conversation !== 12) throw new WsException("Error occured from siam!");
    return { event: "createMessage", data: "Hello world" }
}

Here I throw an WsException when conversation not equal to 12.

In frontend (reactjs) I emiting-

const MessageData = {
    conversation: 16
}
socket.emit("createMessage", MessageData)

Here I sent 16 as conversation. It should throw an error from backend.

How can I get that error from frontend. Actually my question is where I found that WsException error in react application. I have not find any idea about. can anybody help me about this case, please.


Solution

  • You should listen on a specific event: exception.