使用流式JSON来加速你的网站
Updated Sunday, February 16, 2020
更新日期:2020年2月16日,星期日
Modern websites are complex and often need to load data in the background in an asynchronous way. JSON streaming is a way to make this happen.
现代网站很复杂,经常需要在后台以异步的方式加载数据。JSON流是实现这种情况的一种方式。
What is JSON streaming?
什么是JSON流?
JSON streaming–or JSONS–is a simple technique we use to decrease search latency for users, particularly anyone on a slow connection. You'll see the most improvement for mobile users on HTTPS connections, but it really benefits everyone. When you’re working with JSONS, you don't need complicated JSON parsers–just send line-delimited JSON. The Twitter streaming API has worked like this for years.
JSON流--或JSON--是一种简单的技术,我们用来减少用户的搜索延迟,特别是在慢速连接上的人。你会看到移动用户在HTTPS连接上得到最大的改善,但它确实让所有人受益。当你使用JSON时,你不需要复杂的JSON分析器,只需发送以行为单位的JSON即可。Twitter的流媒体API多年来一直是这样工作的。
Why You Should Use a JSON Stream
为什么你应该使用JSON流
Our social username checker offers a simple example of how this works . In it, we check to see if your search is available on Facebook, Twitter and Pinterest by doing an HTTP HEAD request for the name at each site. Theresults come in random order, and we want to show them as fast as possible. We couldwork around this by sending three HTTPS requests, but that means three TCP connections, three SSL handshakes, and so on. We can avoid this–and probably recycle an existing HTTPS connection–if we just send one request:
我们的社会化用户名检查器提供了一个简单的例子,说明了这是如何工作的。在这个例子中,我们通过对每个网站的名字做HTTP HEAD请求,来检查你的搜索是否在Facebook、Twitter和Pinterest上可用。结果是随机的,我们希望尽可能快地显示它们。我们可以通过发送三个HTTPS请求来解决这个问题,但这意味着三个TCP连接,三个SSL握手,等等。如果我们只发送一个请求,就可以避免这种情况,而且还可以回收现有的HTTPS连接。
https://instantdomainsearch.com/services/vanity/apple?hash=866016287
https://instantdomainsearch.com/services/vanity/apple?hash=866016287
The streaming response is analogous to—and relies on—HTTP chunked transfer encoding. The best way to see how this works is to trace the connection with curl:
流式响应类似于并依赖于HTTP的分块传输编码。看这是如何工作的最好方法是用curl追踪连接。
curl --trace - "https://instantdomainsearch.com/services/vanity/apple?hash=866016287"
curl --trace ...