- SG: +65 64383504
- IN: 022 25771219
- IN: 022 25792714
- IN: +91 9987536436
NGINX as a WebSocket Proxy
Sandeep Khuperkar I CTO and Director, Ashnik
The WebSocket specification developed as part of the HTML5 initiative—which defines a full-duplex single socket connection over which messages can be sent between client and server.
HTML5 WebSockets defines an API that enables web pages to use the WebSockets protocol for 2 way communication with remote host. HTML5 WebSockets based applications place less burden on servers, allowing the servers to support more concurrent connections.
WebSocket also provides ability to traverse firewall and proxies, it detects the presence of proxy server and sets up a tunnel to pass through the proxy.
For enterprise production use, where multiple WebSocket servers are needed for performance and high availability, a load balancing layer that understands theWebSocket protocol is required and NGINX has supported WebSocket since NGINX 1.3 and can act as a reverse proxy and do load balancing of WebSocket applications.
WebSocket handshake is compatible with HTTP, using the HTTP Upgrade facility to upgrade the connection from HTTP to WebSocket. This allows WebSocket applications to easily fit into existing infrastructures. For example, WebSocket applications can use the standard HTTP ports 80 and 443, thus allowing the use of existing firewall rules
A WebSocket application keeps a long-running connection open between the client and the server, facilitating the development of real-time applications
As an example suppose for an application total HTTP request and response header information contains 200 bytes ( does not include data). So, what happens when we deploy this application to 10,000 users? Let us the calculation.
10,000 users polling every second: Network throughput ( 200 x 10,000 ) = 2,000,000 bytes
Now if we rebuild the application to use HTML 5 WebSockets, each of the message is WebSocket frame that would have just two bytes of overhead ( instead of 200 bytes ). The same cases would look as below with WebSockets
10,000 users polling every second: Network throughput ( 2 x 10,000 ) = 20,000 bytes
As we can see in above case HTML5 WebSocket provides steep reduction of unnecessary network traffic and which in turn helps in performance improvement.