[英]minimize latency and bandwidth usage between node.js server
I have a node.js script which sends data frequently to a node.js server. 我有一个node.js脚本,该脚本经常将数据发送到node.js服务器。 Right now client is using node.js
request
module to send post request and server is handling through request http server. 现在,客户端正在使用node.js
request
模块发送发布请求,服务器正在通过请求http服务器进行处理。
client: 客户:
const request = require('request');
// some iteration over time for sending event
request.post('http://localhost:8080/event',
{ json: true, body: body },
(err, res, body) => {
callback(err)
})
Server: 服务器:
const http = require('http')
const server = http.createServer(function(req, res) {
let body = []
req.on('data', body.push.bind(body))
req.on('end', () => {
console.log(Buffer.concat(body).toString())
res.end()
})
})
I want to minimize the bandwidth usage and latency. 我想最小化带宽使用和延迟。
Here are my thoughts: 这是我的想法:
I was thinking to use http/2 as it may have better performance (not sure, just from some r&d) but it's still experimental in node.js. 我正在考虑使用http / 2,因为它可能具有更好的性能(不确定,只是从某些研发中得出),但是在node.js中仍处于试验阶段。
right now it's using http, will websocket make any difference on that case as socket can send data over single connection without sending headers all the time. 现在它正在使用http,websocket在这种情况下不会有任何区别,因为套接字可以通过单个连接发送数据而无需始终发送标头。
what will be the best approach to minimize the latency and bandwidth usage? 最小化延迟和带宽使用的最佳方法是什么?
Note: I am not expecting to use any third party service provider for that (want to improve from coding perspective). 注意:我不希望为此使用任何第三方服务提供商(从编码角度来看会有所改善)。
If the question is WebSocket vs. HTTP/2, I'd go with WebSocket. 如果问题是WebSocket与HTTP / 2,我将使用WebSocket。
WebSocket frames are a bit smaller, 2-14 bytes of overhead per frame, compared to HTTP/2's fixed 9. For small messages therefore WebSocket offers less overhead. 与HTTP / 2的固定9相比,WebSocket帧要小一些,每帧的开销为2-14字节。因此,对于较小的消息,WebSocket提供的开销较小。 Which should result in better performance 1 .
这应该导致更好的性能1 。
As of right now, WebSocket is still better supported in servers, proxies and firewalls. 截至目前,服务器,代理和防火墙仍更好地支持WebSocket。
If the question is more general "how to reduce latency", then I'd abandon JavaScript and switch to C++ and zero-copy binary communication. 如果问题是更一般的“如何减少延迟”,那么我将放弃JavaScript并转而使用C ++和零复制二进制通信。
1 Though, to be sure, benchmark both and compare. 1尽管可以肯定地将两者进行基准测试并进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.