The events that a WebSocket object instance fires are:
|called when the connection has been opened|
|called when a message has been received from the server|
|called when an error occurs with the socket (sending or receiving)|
|called when the connection is closed|
For each of these events, you can add an event listener using
, or you can set a corresponding handler directly on the worker object instance, including
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.
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.