You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: all.html
+40-20Lines changed: 40 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -4857,13 +4857,33 @@ <h2>Why use WebSockets?</h2>
4857
4857
web browser does not have to constantly ask for updates through a stream
4858
4858
of AJAX requests.</p>
4859
4859
<p><imgsrc="theme/img/websockets-flow.png" width="100%" alt="WebSockets are more efficient than long polling for server sent updates." class="technical-diagram" /></p>
4860
-
<p>The WebSockets approach for server pushed updates works well for certain
4861
-
categories of web applications such as chat room, which is why that's often
4862
-
the example application for a WebSocket library.</p>
4863
-
<p>Both a JavaScript library on the web browser and a WebSockets
4864
-
implementation on the server are necessary to establish and maintain the
4865
-
connection between the client and server. Examples of JavaScript client
4866
-
libraries and WSGI implementations can be found below.</p>
4860
+
<p>While the above diagram shows a server pushing data to the client, WebSockets
4861
+
is a full-duplex connection so the client can also push data to the server
4862
+
as shown in the diagram below.</p>
4863
+
<p><imgsrc="theme/img/websockets-flow-with-client-push.png" width="100%" alt="WebSockets also allow client push in addition to server pushed updates." class="technical-diagram" /></p>
4864
+
<p>The WebSockets approach for server- and client-pushed updates works well for
4865
+
certain categories of web applications such as chat room, which is why that's
4866
+
often an example application for a WebSocket library.</p>
4867
+
<h2>Implementing WebSockets</h2>
4868
+
<p>Both the web browser and the server must implement the WebSockets protocol
4869
+
to establish and maintain the connection. There are important implications for
4870
+
servers since WebSockets connections are long lived, unlike typical HTTP
4871
+
connections. </p>
4872
+
<p>A multi-threaded or multi-process based server cannot scale appropriately for
4873
+
WebSockets because it is designed to open a connection, handle a request as
4874
+
quickly as possible and then close the connection. An asynchronous server such
4875
+
as <ahref="http://www.tornadoweb.org/en/stable/">Tornado</a> or
4876
+
<ahref="http://gunicorn.org/">Green Unicorn</a> monkey patched with
4877
+
<ahref="http://www.gevent.org/">gevent</a> is necessary for any practical WebSockets
4878
+
server-side implementation.</p>
4879
+
<p>On the client side, it is not necessary to use a JavaScript library for
4880
+
WebSockets. Web browsers that implement WebSockets will expose all necessary
4881
+
client-side functionality through the WebSockets object. However, a JavaScript
4882
+
wrapper library can make a developer's life easier by implementing graceful
4883
+
degradation (often falling back to long-polling when WebSockets are
4884
+
not supported) and by providing a wrapper around browser-specific WebSocket
4885
+
quirks. Examples of JavaScript client libraries and WSGI implementations
Copy file name to clipboardExpand all lines: source/content/pages/04-web-development/1201-websockets.markdown
+45-20Lines changed: 45 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,14 +44,39 @@ of AJAX requests.
44
44
45
45
<imgsrc="theme/img/websockets-flow.png"width="100%"alt="WebSockets are more efficient than long polling for server sent updates."class="technical-diagram" />
46
46
47
-
The WebSockets approach for server pushed updates works well for certain
48
-
categories of web applications such as chat room, which is why that's often
49
-
the example application for a WebSocket library.
47
+
While the above diagram shows a server pushing data to the client, WebSockets
48
+
is a full-duplex connection so the client can also push data to the server
49
+
as shown in the diagram below.
50
50
51
-
Both a JavaScript library on the web browser and a WebSockets
52
-
implementation on the server are necessary to establish and maintain the
53
-
connection between the client and server. Examples of JavaScript client
54
-
libraries and WSGI implementations can be found below.
51
+
<imgsrc="theme/img/websockets-flow-with-client-push.png"width="100%"alt="WebSockets also allow client push in addition to server pushed updates."class="technical-diagram" />
52
+
53
+
The WebSockets approach for server- and client-pushed updates works well for
54
+
certain categories of web applications such as chat room, which is why that's
55
+
often an example application for a WebSocket library.
56
+
57
+
58
+
## Implementing WebSockets
59
+
Both the web browser and the server must implement the WebSockets protocol
60
+
to establish and maintain the connection. There are important implications for
61
+
servers since WebSockets connections are long lived, unlike typical HTTP
62
+
connections.
63
+
64
+
A multi-threaded or multi-process based server cannot scale appropriately for
65
+
WebSockets because it is designed to open a connection, handle a request as
66
+
quickly as possible and then close the connection. An asynchronous server such
67
+
as [Tornado](http://www.tornadoweb.org/en/stable/) or
68
+
[Green Unicorn](http://gunicorn.org/) monkey patched with
69
+
[gevent](http://www.gevent.org/) is necessary for any practical WebSockets
70
+
server-side implementation.
71
+
72
+
On the client side, it is not necessary to use a JavaScript library for
73
+
WebSockets. Web browsers that implement WebSockets will expose all necessary
74
+
client-side functionality through the WebSockets object. However, a JavaScript
75
+
wrapper library can make a developer's life easier by implementing graceful
76
+
degradation (often falling back to long-polling when WebSockets are
77
+
not supported) and by providing a wrapper around browser-specific WebSocket
78
+
quirks. Examples of JavaScript client libraries and WSGI implementations
79
+
are found below.
55
80
56
81
57
82
## JavaScript client libraries
@@ -80,22 +105,22 @@ WebSockets proxy.
80
105
81
106
server {
82
107
83
-
# my typical web server configuration goes here
108
+
# typical web server configuration goes here
84
109
85
-
# this section is specific to the WebSockets proxying
86
-
location /socket.io {
87
-
proxy_pass http://app_server_wsgiapp/socket.io;
88
-
proxy_redirect off;
110
+
# this section is specific to the WebSockets proxying
0 commit comments