简体   繁体   English

Streaming API vs Rest API?

[英]Streaming API vs Rest API?

The canonical example here is Twitter's API. 这里的规范示例是Twitter的API。 I understand conceptually how the REST API works, essentially its just a query to their server for your particular request in which you then receive a response (JSON, XML, etc), great. 我从概念上理解REST API是如何工作的,本质上它只是一个查询服务器,用于您的特定请求,然后您可以接收响应(JSON,XML等),非常好。

However I'm not exactly sure how a streaming API works behind the scenes. 但是,我不确定流API如何在幕后工作。 I understand how to consume it. 我理解如何消费它。 For example with Twitter listen for a response. 例如,Twitter听取回复。 From the response listen for data and in which the tweets come in chunks. 从响应中听取数据,并在其中发送推文。 Build up the chunks in a string buffer and wait for a line feed which signifies end of Tweet. 在字符串缓冲区中构建块并等待表示Tweet结束的换行符。 But what are they doing to make this work? 但他们正在做些什么来使这项工作?

Let's say I had a bunch of data and I wanted to setup a streaming API locally for other people on the net to consume (just like Twitter). 假设我有一堆数据,我想在本地设置一个流API,供网络上的其他人使用(就像Twitter一样)。 How is this done, what technologies? 这是怎么做的,有哪些技术? Is this something Node JS could handle? 这是Node JS可以处理的吗? I'm just trying to wrap my head around what they are doing to make this thing work. 我只是想绕过他们正在做的事情来使这件事发挥作用。

Twitter's stream API is that it's essentially a long-running request that's left open, data is pushed into it as and when it becomes available. Twitter的流API是它本质上是一个长时间运行的请求,它保持打开状态,数据在它可用时被推送到它。

The repercussion of that is that the server will have to be able to deal with lots of concurrent open HTTP connections (one per client). 这种情况的后果是服务器必须能够处理大量并发的开放HTTP连接(每个客户端一个)。 A lot of existing servers don't manage that well, for example Java servlet engines assign one Thread per request which can (a) get quite expensive and (b) quickly hits the normal max-threads setting and prevents subsequent connections. 许多现有的服务器管理得不好,例如Java servlet引擎为每个请求分配一个线程,这可能会(a)变得非常昂贵,并且(b)快速达到正常的最大线程设置并阻止后续连接。

As you guessed the Node.js model fits the idea of a streaming connection much better than say a servlet model does. 正如您所猜测的那样,Node.js模型比使用servlet模型更符合流式连接的想法。 Both requests and responses are exposed as streams in Node.js, but don't occupy an entire thread or process, which means that you could continue pushing data into the stream for as long as it remained open without tying up excessive resources (although this is subjective). 请求和响应都作为Node.js中的流公开,但不占用整个线程或进程,这意味着只要数据保持打开状态就可以继续将数据推送到流中,而不会占用过多的资源(尽管如此是主观的)。 In theory you could have a lot of concurrent open responses connected to a single process and only write to each one when necessary. 从理论上讲,您可以将大量并发开放响应连接到单个进程,并在必要时仅写入每个进程。

If you haven't looked at it already the HTTP docs for Node.js might be useful. 如果你还没有看过它,那么Node.jsHTTP文档可能会有用。

I'd also take a look at technoweenie's Twitter client to see what the consumer end of that API looks like with Node.js, the stream() function in particular . 我还要看一下technoweenie的Twitter客户端 ,看看该API的消费者端是什么样的Node.js, 特别是stream()函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM