Demo
Git repo, create basic and secure websocket server and client via Python websockets module: https://github.com/chengdol/websocket-demo
Introduction
Difference between socket and websocket?
如果你已经理解了HTTP, HTTP CONNECT method 以及Proxy的知识,那么维基百科的描述已经比较清楚了. WebSocket Wiki WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection.
WebSocket is distinct from HTTP (half-duplex, clients issue request). Both protocols are located at layer 5 in the OSI model and depend on TCP at layer 4. Although they are different, RFC 6455 states that WebSocket “is designed to work over HTTP ports 443 and 80 as well as to support HTTP proxies and intermediaries,” thus making it compatible with the HTTP protocol. To achieve compatibility, the WebSocket handshake uses the HTTP(1.1 version) Upgrade header to change from the HTTP protocol to the WebSocket protocol.
Some proxies also support WebSocket, for example, Envoy, some may not.
WebSocket handshake ws://
or wss:// (WebSocket Secure)
:
1 | // client request |
WebSockets use cases:
- chatting
- live feed
- multiplayer gaming
- showing client progress/logging
Pros:
- full-duplex (no polling)
- HTTP compatible
- firewall friendly
Cons:
- lots of proxies and transparent proxies don’t support it yet
- L7, LB challenging (timeouts)
- stateful, difficult to horizontal scale
Do you have to use Web Sockets ? (alternatives) It is important to note that WebSockets is not the only HTTP realtime based solution, there are other ways to achieve real time such as eventsource, and long polling.