

Client Side HTML & JavaScript CodeĪt the time of writing this tutorial, there are only few web browsers supporting WebSocket() interface. After the handshake, either side can send data. The socket starts out as a HTTP connection and then "Upgrades" to a TCP socket after a HTTP handshake. The close() method would be used to terminate any existing connection.Ī WebSocket is a standard bidirectional TCP socket between the client and the server. The send(data) method transmits data using the connection.

Assuming we created Socket object as mentioned above − Sr.No. This event occurs when connection is closed.įollowing are the methods associated with WebSocket object. This event occurs when there is any error in communication. This event occurs when client receives data from server. This event occurs when socket connection is established. Assuming we created Socket object as mentioned above − Event The readonly attribute bufferedAmount represents the number of bytes of UTF-8 text that have been queued using send() method.įollowing are the events associated with WebSocket object. It can have the following values −Ī value of 0 indicates that the connection has not yet been established.Ī value of 1 indicates that the connection is established and communication is possible.Ī value of 2 indicates that the connection is going through the closing handshake.Ī value of 3 indicates that the connection has been closed or could not be opened. The readonly attribute readyState represents the state of the connection. WebSocket Attributesįollowing are the attribute of WebSocket object. The second attribute, protocol is optional, and if present, specifies a sub-protocol that the server must support for the connection to be successful. Here first argument, url, specifies the URL to which to connect. Once you get a Web Socket connection with the web server, you can send data from browser to server by calling a send() method, and receive data from server to browser by an onmessage event handler.įollowing is the API which creates a new WebSocket object. WebSockets is a next-generation bidirectional communication technology for web applications which operates over a single socket and is exposed via a JavaScript interface in HTML 5 compliant browsers.
