Simulating Hard Websocket Disconnect in a Browser

in Uncategorised


I need to test ping pong detecting a connection loss. Unline a soft, programmatical disconnect (.close()), a hard disconnect means network connection loss and won't notify the websocket server.

Chrome's built-in network throttle doesn't affect the existing, already established websocket connections - even with the latest (Feb 2022) update on the issue.

My setup is this:

  • phpStorm for running the debug Node.js server
  • Node inside a VirtualBox Linux machine
  • Chrome browser as a websocket client

We need to take Chrome offline in a way that won't trigger the .close() on our established websocket connection.

  • Disconnecting the Virtual Machine won't help because it will also kill the Node environment
  • Disabling Cable Connected in VM settings also stops phpStorms debug cycle
  • Deleting the port forward in the VM does nothing for an existing connection
  • This post, suggests killing the connection itself on the OS level, which is a good idea.

On our local Linux node server, provided it listens on port 8080, block it with iptables

iptables -A INPUT -p tcp --dport 8080 -j DROP

This will immediately block the external connections from Chrome to that port, not leaving it a chance to close gracefully.

When you need to reconnect, or once you're done testing,

iptables -F
#node-js #websockets #chrome