The events that a WebSocket object instance fires are:
Event | Description |
---|
open | called when the connection has been opened |
message | called when a message has been received from the server |
error | called when an error occurs with the socket (sending or receiving) |
close | called when the connection is closed |
For each of these events, you can add an event listener using
addEventListener(...)
, or you can set a corresponding handler directly on the worker object instance, including
onopen
,
onmessage
,
onerror
and
onclose
.
If Web Sockets are not supported, you’ll need to provide some fallback functionality for your application, or at least gracefully notify the user that their browser doesn’t support the required functionality.
Note:
Note
Because consistent browser support for Web Sockets is minimal, the best practice suggestion for using Web Sockets is to use a library like Socket.io (http://socket.io/), which attempts to use Web Sockets if available, and falls back to a variety of other techniques for communication if Web Sockets are not present.