html-websocket
Introduction
WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.
WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application
. The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request
. The WebSocket protocol makes more interaction between a browser and a web server possible, facilitating the real-time data transfer from and to the server. This is made possible by providing a standardized way for the server to send content to the browser without being solicited by the client, and allowing for messages to be passed back and forth while keeping the connection open. In this way, a two-way (bi-directional) ongoing conversation can take place between a browser and the server. The communications are done over TCP port number 80, which is of benefit for those environments which block non-web Internet connections using a firewall. Similar two-way browser-server communications have been achieved in non-standardized ways using stopgap technologies such as Comet.
The WebSocket protocol specification defines ws and wss as two new uniform resource identifier (URI) schemes that are used for unencrypted and encrypted connections, respectively. Apart from the scheme name and fragment (# is not supported), the rest of the URI components are defined to use URI generic syntax.
WebSockets have to establish a connection to a server for data to flow. WebSockets only establish this connection once by HTTP Method
, then all data is sent over this open WS protocol connection(ws as tcp payload)
. This means each event being sent takes very little resources from both the server and the client because a new connection never has to be established, and ws protocol is small.
It uses HTTP protocol to setup the connection, then switch to WS protocol for data transmission. actually when lower TCP connection is setup, client sends HTTP method
to server, this is handled by websocket, hence the TCP connection is not closed, now client and server can send ws protocl for data transmission)(TCP+ws+payload
)
So itβs very useful for server driver event, like to notify update to client(upgrade notification, stock price update etc)
Websocket event
When it comes to Web Socket events there are mainly four events. They are:
- Open: acts as a handshake between client and server
- Message: happens when the server sends some data. Messages can be plain text messages or binary data
- Close: This marks the end of communication between server and client.
- Error: When an error occurs, when a communication channel is opened then the error event occurs
Connectino setup
client request
1 | GET / HTTP/1.1 |
server response
1 | HTTP/1.1 101 Switching Protocols |
Data transmission
websocket protocol is very short, its header only takes 8 bytes, two import ones
- Opcode: playload type(text, binary)
- Payload length: len of payload
server to client
1 | Frame 7: 79 bytes on wire (632 bits), 79 bytes captured (632 bits) |
client to server
1 | Frame 9: 72 bytes on wire (576 bits), 72 bytes captured (576 bits) |
Examples
client side in browser you can use other web socket client without browser
1 | var ws = new WebSocket("wss://echo.websocket.org"); |
server side
1 | // use websocket library to setup a server |