Websocket
https://github.com/socketio/engine.io#goals
Websocket是SocketIO庫依賴的B/S新特性,它有一些優點。
WebSocket based connections have two fundamental benefits:
- Better server performance
- A: Load balancers
Load balancing a long polling connection poses a serious architectural nightmare since requests can come from any number of open sockets by the user agent, but they all need to be routed to the process and computer that owns theEngine
connection. This negatively impacts RAM and CPU usage.- B: Network traffic
WebSocket is designed around the premise that each message frame has to be surrounded by the least amount of data. In HTTP 1.1 transports, each message frame is surrounded by HTTP headers and chunked encoding frames. If you try to send the message "Hello world" with xhr-polling, the message ultimately becomes larger than if you were to send it with WebSocket.- C: Lightweight parser
As an effect of B, the server has to do a lot more work to parse the network data and figure out the message when traditional HTTP requests are used (as in long polling). This means that another advantage of WebSocket is less server CPU usage.
Better user experience
Due to the reasons stated in point 1, the most important effect of being able to establish a WebSocket connection is raw data transfer speed, which translates in some cases in better user experience.
Applications with heavy realtime interaction (such as games) will benefit greatly, whereas applications like realtime chat (Gmail/Facebook), newsfeeds (Facebook) or timelines (Twitter) will have negligible user experience improvements.
平滑降級
但是在一些條件下,Websocket功能沒有被開啟。
Having said this, attempting to establish a WebSocket connection directly so far has proven problematic:
Proxies
Many corporate proxies block WebSocket traffic.Personal firewall and antivirus software
As a result of our research, we've found that at least 3 personal security applications block WebSocket traffic.Cloud application platforms
Platforms like Heroku or No.de have had trouble keeping up with the fast-paced nature of the evolution of the WebSocket protocol. Applications therefore end up inevitably using long polling, but the seamless installation experience of Socket.IO we strive for ("require() it and it just works") disappears.
這樣會影響用戶體驗
From the user perspective, an unsuccessful WebSocket connection can translate in up to at least 10 seconds of waiting for the realtime application to begin exchanging data. This perceptively hurts user experience.
To summarize, Engine focuses on reliability and user experience first, marginal potential UX improvements and increased server performance second.
Engine
is the result of all the lessons learned with WebSocket in the wild.
架構設計上,協議可以在運行過程切換。
The main premise of
Engine
, and the core of its existence, is the ability to swap transports on the fly. A connection starts as xhr-polling, but it can switch to WebSocket.
支持的協議
Transports
polling
: XHR / JSONP polling transport.websocket
: WebSocket transport.
Protocal fallback
https://github.com/socketio/engine.io-protocol
所以,當Websocket功能失效,系統自動降級為ajax輪訓方式。
A connection always starts with WebSocket (if supported by the client).
If the connection cannot be established, the client will try to establish a SSE stream.
If the connection still fails, the client will use polling as a fallback (either XHR or JSONP).