Intercept WebSocket Server Events with MSW

Share this video with your friends

Social Share Links

Send Tweet

Now that we have the connection to the actual WebSocket server established, we can listen to any events sent from the server to the client. The thing is, our event handler in MSW will receive those events before the client does.

Resources

[00:00] Once the actual WebSocket server connection has been established, we can handle the events sent and received from that server in a similar way like we did with the WebSocket client. To react to a message sent from the original server, you can call server.addEventListener and add a MessageEventListener. It will also accept the message event reference and we can handle it for example by logging it out in the console. So now if you go to the browser and send something in the chat, you can see that message actually being sent over the WebSocket protocol to the original server but also that message event instance being printed here in the console from our handlers.ts module.