使用服务器发送的事件来简化实时流的规模
When building any kind of real-time data application, trying to figure out how to send messages from the server to the client (or vice versa) is a big part of the equation. Over the years, various communication models have popped up to handle server-to-client communication, including Server Sent Events (SSE).
当建立任何一种实时数据应用时,试图找出如何从服务器发送消息到客户端(或反过来)是一个很大的问题。多年来,出现了各种通信模型来处理服务器到客户端的通信,包括服务器发送事件(SSE)。
SSE is a unidirectional server push technology that enables a web client to receive automatic updates from a server via an HTTP connection. With SSE data delivery is quick and simple because there’s no periodic polling, so there’s no need to temporarily stage data.
SSE是一种单向的服务器推送技术,使网络客户端能够通过HTTP连接接收来自服务器的自动更新。有了SSE,数据的传递就变得快速而简单,因为没有周期性的轮询,所以不需要临时放置数据。
This was a perfect addition to a real-time data visualization product Shopify ships every year—our Black Friday Cyber Monday (BFCM) Live Map.
这是对Shopify每年推出的实时数据可视化产品--我们的黑色星期五网络星期一(BFCM)实时地图的一个完美补充。
Our 2021 Live Map system was complex and used a polling communication model that wasn’t well suited. While this system had 100 percent uptime, it wasn't without its bottlenecks. We knew we could improve performance and data latency.
我们的2021年实时地图系统很复杂,使用的是不太适合的投票通信模式。虽然这个系统有100%的正常运行时间,但它也不是没有瓶颈的。我们知道我们可以改善性能和数据延迟。
Below, we’ll walk through how we implemented an SSE server to simplify our BFCM Live Map architecture and improve data latency. We’ll discuss choosing the right communication model for your use case, the benefits of SSE, and code examples for how to implement a scalable SSE server that’s load-balanced with Nginx in Golang.
下面,我们将介绍我们如何实现SSE服务器,以简化我们的BFCM Live Map架构并改善数据延迟。我们将讨论为你的用例选择正确的通信模型,SSE的好处,以及如何在Golang中实现与Nginx负载平衡的可扩展SSE服务器的代码示例。
Choosing a Real-time Communication Model
选择一个实时通信模式
First, let’s discuss choosing how to send messages. When it comes to real-time data streaming, there are three communication models:
首先,让我们讨论一下选择如何发送消息。当涉及到实时数据流时,有三种通信模式。
- Push: This is the most real-time model. The client opens a connection to the server and that connecti...