简体   繁体   中英

Alternatives to HTTP chunking for persistent connections?

According to the W3C specification on Server-Sent Events :

Authors are also cautioned that HTTP chunking can have unexpected negative effects on the reliability of this protocol. Where possible, chunking should be disabled for serving event streams unless the rate of messages is high enough for this not to matter.

How exactly would I go about doing this server-side in node.js? Setting the Content-Length header isn't really an option because the connection should ideally never close. Would I set the response header Transfer-Encoding to identity or remove the Transfer-Encoding header entirely?

Node will use chunked encoding automatically if you don't explicitly provide a length or manually set a transfer encoding. It would also be good to set Connection: close too as you obviously won't be keeping the connection open after when the server finishes sending events.

res.setHeader('Transfer-Encoding', 'identity');
res.setHeader('Connection', 'close');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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