[转帖]Real-Time Web Applications with WebSocket and NGINX

real,time,web,applications,with,websocket,and,nginx · 浏览次数 : 0

小编点评

**Architecture and Infrastructure Issues for Real-Time Applications with WebSocket** **Components:** * Client browser (running WebSocket JavaScript application) * WebSocket server * WebSocket proxy (NGINX) **Structure:** 1. Client establishes a WebSocket connection to the proxy server. 2. Proxy server forwards requests to the backend servers. 3. Proxy server handles WebSocket communication between the client and server. 4. Clients can connect to the proxy server directly or through a load balancer. **Technical Requirements:** * Node.jsRuby, Ruby, PHP, Python, and other WebSocket libraries * WebSocket server libraries (ws, socket.io, etc.) * NGINX server * SSL certificates for the proxy server **Scale and Performance:** * Proxy server can scale to handle a large number of concurrent users. * Low latency is critical for real-time applications. * High availability is essential to minimize downtime. **Security:** * NGINX can act as a WebSocket proxy, ensuring that all communication is encrypted. * Proxy server can implement access control and authentication mechanisms. **Use Cases:** * WebRTC-based applications * Online games * Chat applications * Stock tracking * Sports score updates **Benefits of Using NGINX as a WebSocket Proxy:** * Routing flexibility * SSL termination support * Load balancing * High performance and reliability

正文

 

In the blog post NGINX as a WebSocket Proxy we discussed using NGINX to proxy WebSocket application servers. In this post we will discuss some of the architecture and infrastructure issues to consider when creating real-time applications with WebSocket, including the components you need and how you can structure your systems.

WebSocket Adds Interactivity to HTTP

HTTP works well for web applications that are request/response based, where the communications flow always has the client initiating the request to which a backend server responds. Web applications with a more interactive, message-based interaction between the client and server need something beyond simple HTTP. Previous attempts to simulate full-duplex communication over HTTP were complicated and had many drawbacks. The WebSocket protocol, along with the WebSocket JavaScript interface, provide a much easier way to build these types of applications while also reducing the amount of data transferred and the latency.

The ability to create a full-duplex socket connection between the client and server allows for the development of real-time event-driven web applications that utilize push, poll, or streaming communications. Using WebSocket, either the client or the server can initiate communication after the connection is established. This enables many types of web applications, including online games, chat, stock tracking, and real-time reporting of sports scores.

Another use case for WebSocket is developing WebRTC-based applications. WebRTC is used to create applications that do browser-to-browser communications. It requires a separate signaling channel for setup of communications, and WebSocket is a good protocol to use for this.

Technical Requirements for a WebSocket Application

The minimum infrastructure required for a WebSocket web application is a client browser running a WebSocket JavaScript application and communicating with a WebSocket server. The recent releases of all major browsers now support WebSocket, but it is important to make sure that the clients have a browser with WebSocket support. There are many options for WebSocket servers and here are some examples for different environments (this list is provided for your convenience and doesn’t constitute endorsement by NGINX, Inc.):

 

 

   Node.js Ruby
  ws websocket-rails
  socket.io em-websocket
  SockJS webmachine-ruby
     
  Perl Tomcat
  Mojolicious WebSocket How-To
  Net::WebSocket::Server  
     
  PHP WebLogic
  Ratchet Using WebSockets in WebLogic Server
  PHP-Push-WebSocket  
  PHP-Websockets  
     
  Python  
  websockets  
  ws4py  

Scaling WebSocket to High Traffic Volumes

It’s possible to use a single WebSocket server, but application performance is limited by the capacity of the server, which is also a single point of failure. Many real-time applications are developed with the hope of seeing widespread adoption, but it can difficult to predict the rate of growth. To support the increasing popularity of a real-time application the infrastructure must be able to:

  • Scale to support a large number of concurrent users
  • Provide low latency when handling communications between client and server
  • Support high availability (implying there is no single point of failure)
  • Allow for the configuration to be easily changed and adapt to changing conditions

This can be accomplished by putting NGINX in front of the WebSocket servers and HTTP servers (since most WebSocket applications also use HTTP), to act as a WebSocket-aware reverse proxy and load balancer. Not all reverse proxies provide direct support for WebSocket. Some, such as Elastic Load Balancing (ELB) in the Amazon Web Services (AWS) environment, have to run in TCP mode, meaning that they cannot deal with HTTP headers. The Proxy Protocol was developed to deal with this limitation and NGINX supports it as well, meaning WebSocket is fully supported in AWS. Using NGINX as a WebSocket reverse proxy brings additional benefits, such as routing flexibility and the ability to have NGINX terminate SSL connections.

A typical WebSocket configuration using NGINX might look something like this:

Here we show a variety of desktop and mobile clients connecting to a highly available pair of NGINX servers that are load balancing a set of HTTP and WebSocket servers. This type of configuration can withstand the failure of a WebSocket server or an NGINX server and you can easily add or remove WebSocket servers. Also, all the routing details of how to connect to the WebSocket servers is hidden from the clients, making for a robust and scalable WebSocket application infrastructure. Web performance and reliability for real-time web applications is critical. Contact us today to learn how NGINX Plus can help improve the performance, reliability, and availability of your applications.

For more information, please see:

与[转帖]Real-Time Web Applications with WebSocket and NGINX相似的内容:

[转帖]Real-Time Web Applications with WebSocket and NGINX

https://www.nginx.com/blog/realtime-applications-nginx/ In the blog post NGINX as a WebSocket Proxy we discussed using NGINX to proxy WebSocket applic

[转帖]使用Red Hat Enterprise Linux的实时内核

运行实时内核并评估其对应用程序的潜力和性能优势是值得的。 https://www.redhat.com/sysadmin/real-time-kernel 目录 什么是实时内核? 实时安装RHEL Wrap up 实时内核功能在开源生态系统中已经存在了十多年。同样,红帽企业Linux(RHEL)对实

[转帖]关于面试时HA(RAC)会问到的一些问题

1.什么是RAC(Real Application Cluster)? RAC(Real Application Cluster)是Oracle数据库的一种部署架构,它将多个数据库服务器连接在一起,共同组成一个实例。这些服务器之间通过高速网络相互通信,共享存储和计算资源,从而提供更高的可用性、性能和

[转帖]浅析Nginx配置获取客户端真实IP的proxy_set_header、X-Real-IP、$remote_addr、X-Forwarded-For、$proxy_add_x_forwarded_for分别是什么意思

https://www.cnblogs.com/goloving/p/15588668.html 一、问题背景 在实际应用中,我们可能需要获取用户的ip地址,比如做异地登陆的判断,或者统计ip访问次数等,通常情况下我们使用 request.getRemoteAddr() 就可以获取到客户端ip,但是

[转帖]Latent Sector Errors, Disk Failure, and RAID Failure (part 1)

本文来自三篇关于磁盘错误的论文: Understanding Latent Sector Errors and How to Protect Against Them, FAST 2010 Disk failures in the real world: What does an MTTF of 1

[转帖]nginx如何实现负载均衡、限流、缓存、黑白名单和灰度发布

https://zhuanlan.zhihu.com/p/464491494 挺好的文章. nginx负载均衡配置 1.负载均衡配置 http { upstream real_server { server 192.168.1.100:8082 weight=1; #轮询服务器和访问权重 serve

[转帖]LVS负载均衡的三种方式

1.VS-NAT(基于网络地址转换,network address translation ,NAT) VS-NAT是LVS最基本的方法,如果想要设置一个用于测试的LVS,这是一个最简单的方法。 当客户发出请求,lvs负载均衡中的director会将接受到的包的目标地址重写为某个real-serve

[转帖]Linux的tmpfs和ramfs

tmpfs tmpfs是一种虚拟内存文件系统, 它的存储空间在VM里面,现在大多数操作系统都采用了虚拟内存管理机制, VM(Virtual Memory) 是由Linux内核里面的VM子系统管理. VM的大小由RM(Real Memory)和swap组成, RM就是物理内存, swap是通过硬盘虚拟

[转帖]

Linux ubuntu20.04 网络配置(图文教程) 因为我是刚装好的最小系统,所以很多东西都没有,在开始配置之前需要做下准备 环境准备 系统:ubuntu20.04网卡:双网卡 网卡一:供连接互联网使用网卡二:供连接内网使用(看情况,如果一张网卡足够,没必要做第二张网卡) 工具: net-to

[转帖]

https://cloud.tencent.com/developer/article/2168105?areaSource=104001.13&traceId=zcVNsKTUApF9rNJSkcCbB 前言 Redis作为高性能的内存数据库,在大数据量的情况下也会遇到性能瓶颈,日常开发中只有时刻