Erroring the connection

You can error the intercepted WebSocket client connection by throwing an error at the root scope of your connection event listener.

api.addEventListener('connection', () => {
  throw new Error('Failed to connect')
})

This will result in the error event being dispatched on your WebSocket client and the underlying connection being closed with the 1011 closure code, if it hasn’t been already. Additionally, the thrown error will be logged to the console for reference.

Note that you cannot access the exact error reference or any information about the error in the error event listener on the client as per WHATWG WebSocket API specification.