How to intercept websocket communication?
I did not find any example/api using which I could intercept websocket communication.
I have a website connecting to remote server through websocket, I want to intercept and listen to the communication, how do I achieve that?
Do you need to listen only or to intercept as well? I had web socket instrumentation in works, but was waiting for the customer.
Actually both.
Would be great if you could share that ?
I only had API for web socket frame inspection, not interception. If that would be helpful - I'll go back to it and finish my implementation for WebKit (I already had it working for Chromium and Firefox). But if you anyways need to mock responses, that's something that would be very hard to do in Chromium, so achieving it could be simpler via mocking the web socket API in the user land instead.
hmm..
My need was to intercept the websocket communication, take action when specific message received, send the message back to the server through the same websocket.. something like man in the middle without disturbing the normal communication.
I looked into Selenium, but there was no easy way to do this. The only way I found is to monkey patch the low level networking APIs.
Something like this: https://stackoverflow.com/questions/57893407/capturing-websocket-messages-from-network-tab https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-webSocketClosed
It would have been better if browser autmation frameworks provide this as built in feature with simpler APIs.
I had hope that Playwright would have had this feature, but sadly no.
DevTools protocol you point at does not have web socket interception, just sniffing, so you can't do man in the middle. But you can totally do it in the userland via calling page.addInitScript and replacing WebSocket class with your own.
Ok, thanks for clarifying. I think what you mention should work. I will try that. Thanks!
@pavelfeldman I've found the pull request you mentioned #542 which is displayed as merged into master. However browsing master I cannot find the docs.
Being able to inspect websocket messages would be great for my use case. However I might need it to work with Firefox.
Edit: I digged a little deeper and found the revert in #961
To sum up the issue:
- the websocket feature is discussed here: https://github.com/microsoft/playwright/issues/2695
- there's also a neat workaround for those who are interested
Let's continue discussion in #2695.