Research of Web Real-Time Communication Based on Web Socket
Total Page:16
File Type:pdf, Size:1020Kb
Int. J. Communications, Network and System Sciences, 2012, 5, 797-801 http://dx.doi.org/10.4236/ijcns.2012.512083 Published Online December 2012 (http://www.SciRP.org/journal/ijcns) Research of Web Real-Time Communication Based on Web Socket Qigang Liu, Xiangyang Sun Sydney Institute of Language & Commerce, Shanghai University, Shanghai, China Email: [email protected] Received September 4, 2012; revised October 15, 2012; accepted October 26, 2012 ABSTRACT Regarding the limitations of traditional web real-time communication solutions such as polling, long-polling, flash plug-in, propose that using new coming Web Socket technology in the web real-time communication field, introduce the features of Web Socket technology, analysis the difference between Web Socket protocol and HTTP protocol, offer an approach to implement the Web Socket both in client and server side, prove Web Socket can decrease network traf- fic and latency greatly by an experiment, made the prospect of future application of Web Socket in web real-time com- munication. Keywords: Web Socket; Full-Duplex; HTTP Streaming; Long-Polling; Latency 1. Introduction mary solutions used by Web developers to accomplish the real-time communication between browser and server The Internet has been an indispensable part of people’s in the past. life in the fast growing information age. People’s re- Polling an approach manually refreshing page is re- quirements for the Internet have changed from informa- placed by auto running program is the earliest solution tion accessibility in the Web 1.0 era to information in- for real-time communication applied in browser. Easy teraction in the Web 2.0 era, and to current instant inter- implementation and no additional requirement for client action observed in an increasing number of pricing sys- and server are the big advantage of this solution. How- tems, e-commerce systems, and news announce systems. ever, there are some obvious shortcomings in this solu- Currently, the communication between client browser tion, it is very hard to figure out the frequency of data and server is based on Hypertext Transfer Protocol (HTTP), updating, so browser can’t get the latest data in time. an Application Layer protocol which is request-response Additionally, in the case of no data updating occurring based and stateless. An HTTP client initiates a request. It during a period of time, browser’s frequent request will establishes a Transmission Control Protocol (TCP) con- generate unnecessary network traffic and cause unneces- nection. After receiving the client’s request message, a sary burden for server. server sends back a message as a response and terminates For making server communicate with browser at any the connection. Under this model, servers can not send time, Web developers design a new visit mechanism real-time data to clients. Therefore, technologies, such as called long-polling or Comet, through which server will Flash, Comet, and Ajax long polling, have been applied save the new request from browser for a period instead of to implement real-time communication between client sending respond right away. If data updating occurs in and server. However, these technologies cannot accom- this period, server will response to browser with the new plish real-time communications, because some of them coming data, and browser will make another request need install plug-ins on browsers, some of them cause when receives the response [1]. By this mechanism, heavy load for server. The emergence of HTML5 and the browser can get the latest data of server side in time. Web Socket protocol realize the real-time data transmis- However, if a large number of concurrency happens, sion in web-based system, so far, they are considered as server memory and computing capacity will be con- the best solution to resolve this issue. sumed greatly by maintaining those live HTTP connec- 2. Traditional Web Real-Time tions. Communication Solutions Developers have also attempt “HTTP Streaming” visit mechanism. Its main difference is server will never close Polling, long polling and HTTP streaming were the pri- a connection sponsored by browser, sever will used this Copyright © 2012 SciRes. IJCNS 798 Q. G. LIU, X. Y. SUN connection for sending message at any time. In this case, From client (browser) to server: since server won’t signal the completion of the connec- GET /long-polling HTTP/1.1 tion, response from server will be probably buffered by Host: www.kaazing.com firewalls and proxy servers in the network, cause some User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; errors happened during browser receiving data. en-US; rv:1.9) Gecko/2008061017 Firefox/3.0 Accept: 3. The Introduction of Web Socket text/html,application/xhtml+xml,application/xml;q = 0.9, */*; q = 0.8 Web Socket, as a new feature of HTML5, is defined as a Accept-Language: en-us,en;q = 0.5 technology that enables web pages to use the Web Socket Accept-Encoding: gzip,deflate protocol for full-duplex communication with a remote Accept-Charset: ISO-8859-1,utf-8;q = 0.7,*;q = 0.7 host. It introduces the Web Socket interface and defines a Keep-Alive: 300 full-duplex communication channel that operates through Connection: keep-alive a single socket over the Web [2]. HTML5 Web Socket Cache-Control: max-age = 0 efficiently provides a socket connection to internet with Referer: http://www.example.com/ minimal overhead. It delivers an enormous reduction in network traffic and latency compared to Ajax polling and From server to client (browser): Comet solutions that are often used to transmit real-time Date: Tue, 16 Aug 2008 00:00:00 GMT date to simulate full-duplex communication by main- Server: Apache/2.2.9 (Unix) taining two HTTP connections. Therefore, it is the ideal Content-Type: text/plain technology for building scalable, real-time web commu- Content-Length: 12 nication system. To use HTML5 Web Socket to connect one web client Hello world with another remote end-point, a new Web Socket in- Shown by the above two headers, apart from the data stance should be initialized with a valid URL that repre- “Hello World”, most of the data in these headers are sents the remote end-point to be connected. Web Socket useless for end user during this interaction between client defines ws:// and wss:// scheme as Web Socket and se- and server, let along the Cookie and Session (information cure Web Socket connection separately. A Web Socket contained in these two items is usually more than control connection is established when updating a HTTP proto- information in headers in most websites). Furthermore, col to Web Socket protocol during the initial handshake these types of headers will be included in each interac- between client and server. tion. So, it must wastes lots of bandwidth, produces a Web Socket connections use standard HTTP ports (80 great number of network traffic if resort to polling and and 443), therefore, it is called “proxy server and fire- Comet solutions. Additionally, constructing and analyz- wall-friendly protocol” [3]. So, HTML5 Web Socket ing the headers will take up some of the time that used to does not require any new hard-ware to be installed. With- process request and response, and lead to some degree of out any intermediate server (proxy or reverse proxy latency. These shortcomings of polling and Comet have server, firewall, load-balance router and so on), a new indicated that these two techniques must be replaced by Web Socket connection can be established successfully, other real-time communication technologies in the future. as long as both client and server support Web Socket Let’s turn to the Web Socket connections. protocol. Web Socket use the HTTP Upgrade mechanism up- 4. Comparison between Web Socket grade to Web Socket protocol [4]. Web Socket’s hand- Connections and HTTP Connections shake mechanism is compatible with HTTP. Therefore, HTTP servers can share the default HTTP and HTTPS Communication between client and server is usually ports (80 and 443) with Web Socket servers. To establish based on HTTP connections which require headers at- a new Web Socket connection, HTTP protocol will be tached to the request of client and response of server, upgraded to Web Socket protocol during the initial hand- according HTTP protocol definition, these headers con- shake between client and server. Once the connection is tain some transmission control information such as pro- established, Web Socket will be transmitted back and tocol type, protocol version, browser type, transmission forth between client and server based on full-duplex language, encoding type, out of time, Cookie and Session. model. The header of initial handshake is given as below: Under the help of software like Firebug and Turning on From client (browser) to server: Live HTTP Headers, headers of request and response can GET /text HTTP/1.1 be observed clearly. An example of one request and re- Upgrade: WebSocket sponse’s headers are defined as follows: Connection: Upgrade Copyright © 2012 SciRes. IJCNS Q. G. LIU, X. Y. SUN 799 Host: www.websocket.org void close(); }; From server to client (browser): According the definition of interface and construct HTTP/1.1 101 WebSocket Protocol Handshake function, a new Web Socket instance can be initialized Upgrade: WebSocket by two parameters, one necessary parameter is a valid net Connection: Upgrade work address, and the other optional parameter is proto- col type. In browsers, Web Socket object is operated by Hello world JavaScript. A new Web Socket instance can be created It is clearly shown that the control information includ- by a piece of simple code: ed in headers of Web Socket connections is much less var myWebSocket = new WebSocket than that in the headers of HTTP connections.