我可以使用http2将响应流式传输回浏览器吗? [英] Can I stream a response back to the browser using http2?

查看:72
本文介绍了我可以使用http2将响应流式传输回浏览器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用http2将响应从节点流回浏览器?

Is it possible to stream a response back to the browser, from node, perhaps using http2?

在我的网络应用中,用户按下一个按钮即可启动服务器进程.此过程可能需要10分钟或更长时间才能完成.我想将状态更新流回客户端/浏览器.

In my web-app, a user presses a button which starts a server process. This process might take 10 minutes or longer to complete. I want to stream status updates back to the client/browser.

我相信我可以使用websockets来做到这一点,但是我希望http2可以使此事情变得更容易.我知道它支持推送",但据我所知,这仅用于推送用户将来可能需要的文件.

I believe I can do this with websockets, but I was hoping http2 had something to make this easier. I know it supports "push" but as far as I know, that's only for pushing files the user might need in the future.

或者也许我什至不需要http2?浏览器将保持打开连接状态多长时间?我可以无限期地保持res.write()吗?

Or maybe I don't even need http2? How long will the browser keep a connection open for? Can I just keep res.write()ing to it indefinitely?

推荐答案

原则上,您可以使用任何HTTP版本将数据从服务器流传输到客户端,只需在响应正文中仅发送数据片段即可.但是,当前的浏览器HTTP API(XHR和fetch)尚不允许以流方式读取响应主体,而是它们将缓冲整个响应(这对于无限流没有意义).当获取API的流功能完全可用时,将来可以使用流响应主体,请参见:

In principal you can stream data from a server to client with any HTTP version, just by sending only fragments of the data within the response body. However the current brower HTTP APIs (XHR and fetch) don't yet allow to read the response body in a streaming fashion, instead they will buffer the whole response (which does not make sense for an infinite stream). Working with a streamed response body will be possible in the future when the streaming feature of the fetch API is fully available, see: https://www.chromestatus.com/feature/5804334163951616 You can already use it on Chrome, but as far as I understand it's not fully standardized and broadly available yet.

此一般规则的例外是服务器发送事件(SSE),它是来自浏览器的HTTP响应,遵循明确定义的转换编码(text/event-stream).浏览器支持SSE API以使用SSE响应,并且SSE API允许您为每个接收到的数据块获取事件.该API非常简单,您无需发明自己的分块机制,因此对于您的应用程序来说可能是一个不错的选择.

An exception to this general rule are Server Sent Events (SSE), which are HTTP responses from the browser which follow a well defined transform encoding (text/event-stream). Browsers support the SSE APIs in order to work with SSE responses, and the SSE APIs allow you to get an event for each received data chunk. The API is fairly easy and you don't need to invent an own chunking mechanism, so it might be a good option for your application.

关于HTTP/1.1和HTTP/2:您将观察到的主要区别是可以使用它们创建的并发流的数量.对于HTTP/1.1,每个流都需要完整的TCP连接,并且浏览器将连接限制到远程主机(我认为少于10个).因此,不可能创建50个SSE流-而且我走得更远,并说您可能应该只使用一个SSE流并将所有事件数据放入其中.另一方面,HTTP/2允许在单个TCP连接上多路复用许多HTTP请求(从而也可以流式传输正文响应),这意味着在那里存在大量并发SSE流的问题不大.

Regarding HTTP/1.1 and HTTP/2: The major difference that you would observe it the number of concurrent streams that you can create with them. For HTTP/1.1 each stream requires a full TCP connection, and browsers limit connections to a remote host (I think to less than 10). So creating 50 SSE streams is not possible - and I would go even further and would say you probably should only use a single SSE stream and put all event data in that. HTTP/2 on the other hand allows to multiplex lots of HTTP requests (and thereby also streamed body responses) on a single TCP connection, which means having a larger number of concurrent SSE streams is less of a problem there.

这篇关于我可以使用http2将响应流式传输回浏览器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆