47

Is it possible that the onerror callback can be called where onclose will not be called immediately after? In other words, is it possible to get a WebSocket error which does not coincide with the connection then being closed?

If it is possible, I would like to test this case. I have a mocked backend which uses node.js and express-ws. What can I do in the backend to trigger the onerror event callback in the front end.

1 Answer 1

53

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:

  1. 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 error at the WebSocket object. [WSP]

Before the third step then calls close:

  1. Fire an event named close at the WebSocket object, using CloseEvent,...

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.

6
  • I think if connection was not possible to establish you might get error and not close because connection was not even opened.
    – Safareli
    Commented Jun 8, 2018 at 19:17
  • @Safareli Have you seen that happen? I think the browser would be behaving in a non standard manner if so - as mentioned in my answer, the only time the spec mentions the error event firing is prior to then also firing close. If you’re actually seeing something different, it would be worth having the details (browser version etc) as it might be a bug. Commented Jun 8, 2018 at 19:26
  • 4
    @Safareli In fact, the spec mentions this if the opening connection fails: ”If the establish a WebSocket connection algorithm fails, it triggers the fail the WebSocket connection algorithm, which then invokes the close the WebSocket connection algorithm, which then establishes that the WebSocket connection is closed, which fires the close event as described below.” - ie the same steps as outlined in my answer, so it really shouldn’t ever fire error by itself. Commented Jun 8, 2018 at 20:04
  • 1
    Actually, when creating too many websockets in IE (max number is 6 in one IE browser), then only an error will be called, not through the onerror function, but as a SCRIPT ERROR (more specific: SCRIPT5022: SecurityError, is the error that is being called). If this happens, the onclose method will not be called!
    – OuuGiii
    Commented Nov 27, 2018 at 11:31
  • @OuuGiii Interesting - guess that falls under my "behaving in a non standard manner" comment above. Guess it's fairly border line though, since it's not the connection that's failed (which is what triggers onerror/onclose), but IE itself refusing to even attempt to open another one. Commented Nov 27, 2018 at 11:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.