Chrome - Disabling Web Sockets or Closing a Web Socket Connection?
Asked Answered
M

6

68

Is there any way to disable web socket connections or end a web socket connection through Chrome's developer tools network tab?

I've noticed that turning throttling under the network tab to Offline doesn't affect web socket connections that have already been established. It only prevents traditional HTTP requests from going out.

There's a question here related to this, but it's woefully outdated.

Mateya answered 2/8, 2016 at 19:34 Comment(1)
Doesn't seem possible. Bitcoinchain is an easy site to test on.Archaeology
C
34

February 2022 update

As of Chrome 99 this is supported: https://developer.chrome.com/blog/new-in-devtools-99/#websocket

Original answer

No, there is no way to disable or close a connection from the Network panel. Source: DevTools Engineer.

If you have a reference to the WS connection, though, you can close it via the Console using its JS API.

Catena answered 2/8, 2016 at 21:32 Comment(5)
whats the technical limitation here? without any further knowledge, I'd imagine it would be easy to just have the offline button disable any network communication at allBeatnik
It's a matter of prioritization. A DevTools engineer said in this comment that the existing offline implementation does not affect web socket connections and that it is not an easy fix. If a lot of DevTools users request the feature, the team would reconsider dedicating resources to fixing it.Catena
This answer can be improved by providing an example of how to find the WebSocket reference and close a WebSocket connection via its JavaScript API.Mechellemechlin
This is now supportedPyrochemical
The newly introduced support is sadly partial: the throttle does not seem to apply to messages within existing connectionsCarefree
V
13

You can close idle and flush your inactive socket pools in the Net Internals page in Chrome. However, this unfortunately only closes your active sockets by the looks of it.

chrome://net-internals/#sockets

Web Sockets

You would have to use the WebSockets API and call close() on a reference to an existing socket to close it explicitly. Otherwise, killing the process with the active socket is all I can think of.

Veldaveleda answered 2/8, 2016 at 23:59 Comment(0)
H
8

Try this, produced by hours of digging for a similar issue I had. Seems to work for me for closing an "existing" connection on demand.

  1. Chrome Dev Tools -> Memory tab -> Take Snapshot
  2. Once completed, search snapshot on summary tab for "websocket"
  3. Expand the results, there might be a few. Usually the one you want is the one that has the correct websocket url field in preview (when you hover over the entry)
  4. Select it, right click and "store as global variable"
  5. In the console that opens up at the bottom, execute a .close() on the temp variable that the connection was stored on. Probably will be a "temp1.close()".

This approach is similar to another post that recommends pulling a reference from console.dir() . I'm not sure why - but console.dir() approach didnt work for me. But this heap reference did. Adding as an alternative.

Hernandes answered 28/6, 2023 at 21:32 Comment(4)
I like this - it works well for me.. thanksPatrilineal
Gread ideas with the global variable. Works also from sources tab, when you can break your code execution on the right spot.Biostatics
Yeah, anywhere you can get a handle on the WebSocket instance. Combining this with throttling seems to be the right solution for me to test reconnection behavior.Piecemeal
Combined with https://mcmap.net/q/293176/-chrome-disabling-web-sockets-or-closing-a-web-socket-connection you can do queryObjects(WebSocket) to list websockets, right-click one to store as global variable like temp1, then call temp1.close().Wills
R
2

The only way is to cut down the connection for entire tab.

enter image description here

Rodmur answered 8/6, 2023 at 4:6 Comment(1)
no work for established connectionDorie
P
0

I assume you want to test unexpected connection failures, it is possible, but not through Network-tab and you need to be able to log from code.

You can close Websocket connections if you are able to use console.dir(socket) even when you could not store the connection reference, e.g. due sandboxing. Via console.dir() you are able to gain a reference to the socket by right clicking the console.dir() output of the websocket and choosing "Store object as global variable". Then call close() on the temporary reference and the connection you want to terminate closes.

Dev tools close connection

Physostomous answered 10/11, 2022 at 22:29 Comment(0)
T
0

similar to @Tomi Heiskanen answer, but without needing to log from the code. From the devtools console, you can get a reference to the WebSocket by using queryObjects (https://developer.chrome.com/docs/devtools/console/utilities#queryObjects-function).

queryObjects(WebSocket)

this will print on the console all the objects created whose constructor was WebSocket.

(The following steps are the same as @Tomi Heiskanen answer)

Then you can right-click on the websocket you want to use, in case you have many, and Store object as global variable, which creates some variable and prints it on the console again (e.g. temp1). Finally, you can do temp1.close().

Technics answered 5/4 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.