The error event will only ever be fired prior to then also firing the close event, at least by implementations that properly implement the specification, i.e. you will get error and close as a pair, or just close by itself.
The process for closing a websocket consists of 3 steps, the second of which optionally fires the error event if needed:
- If the user agent was required to fail the WebSocket connection, or if the the WebSocket connection was closed after being flagged as full, fire a simple event named
errorat the WebSocket object. [WSP]
Before the third step then calls close:
- Fire an event named
closeat the WebSocket object, usingCloseEvent,…
And because both of these steps are one after the other inside the same queued task, your event handlers should be invoked one after the other, with no other events or tasks taking place in between them.