用于实时数据的 websocket 与 rest API? [英] websocket vs rest API for real time data?

查看:90
本文介绍了用于实时数据的 websocket 与 rest API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要不断访问服务器以获取金融工具的实时数据.价格在不断变化,所以我需要每 0.5 秒请求一次新价格.代理的 REST API 允许我这样做,但是,我注意到连接到服务器时有相当长的延迟.我只是注意到他们也有 websocket API.根据我的阅读,他们都有一些优点/缺点.但是对于我想做的事情并且因为速度在这里特别重要,您会推荐哪种 API?websocket 真的更快吗?

I need to constantly access a server to get real time data of financial instruments. The price is constantly changing so I need to request new prices every 0.5 seconds. The REST APIs of the brokers let me do this, however, I have noticed there's quite some delay when connecting to the server. I just noticed that they also have websocket API though. According to what I read, they both have some pros/cons. But for what I want to do and because speed is specially important here, which kind if API would you recommend? Is websocket really faster?

谢谢!

推荐答案

最有效的操作是在客户端和服务器之间使用 webSocket 连接,并让服务器直接向客户端发送更新的价格信息仅当价格发生某种有意义的变化或经过了某个最短时间且价格发生变化时,才通过 webSocket.

The most efficient operation for what you're describing would be to use a webSocket connection between client and server and have the server send updated price information directly to the client over the webSocket ONLY when the price changes by some meaningful amount or when some minimum amount of time has elapsed and the price has changed.

这可能比让客户不断要求新的价格变化更有效率,并且新信息到达客户的时间可以更及时.

This could be much more efficient than having the client constantly ask for new price changes and the timing of when the new information gets to the client can be more timely.

因此,如果您对新价格水平的信息到达客户端的速度感兴趣,webSocket 可以更及时地将其到达那里,因为服务器可以直接将新价格信息发送给客户端它在服务器上发生变化的那一刻.而使用 REST 调用时,客户端必须在某个固定的时间间隔内进行轮询,并且只会在其轮询间隔时间点获取新数据.

So, if you're interested in how quickly the information on a new price level gets to the client, a webSocket can get it there much more timely because the server can just send the new pricing information directly to the client the very moment it changes on the server. Whereas using a REST call, the client has to poll on some fixed time interval and will only ever get new data at the point of their polling interval.

webSocket 还可以在您的网络基础设施上更快、更轻松,因为只需通过已经打开的 webSocket 连接发送数据包所涉及的网络操作更少,而不是为每个 REST/Ajax 调用创建一个新连接,发送新数据,然后关闭连接.这会对您的特定应用程序产生多大的不同/改进,您必须衡量才能真正了解.

A webSocket can also be faster and easier on your networking infrastructure simply because fewer network operations are involved to simply send a packet over an already open webSocket connection versus creating a new connection for each REST/Ajax call, sending new data, then closing the connection. How much of a difference/improvement this makes in your particular application would be something you'd have to measure to really know.

但是,webSockets 旨在帮助解决您的特定场景,其中客户端想要知道(尽可能接近实时)服务器上的某些内容何时发生变化,因此我肯定会认为这将是首选的设计模式这种用途.

But, webSockets were designed to help with your specific scenario where a client wants to know (as close to real-time as practical) when something changes on the server so I would definitely think that it would be the preferred design pattern for this type of use.

这里比较了通过已经打开的 webSocket 发送价格变化与进行 REST 调用所涉及的网络操作.

Here's a comparison of the networking operations involved in sending a price change over an already open webSocket vs. making a REST call.

webSocket

  1. 服务器发现价格发生变化并立即向每个客户端发送消息.
  2. 客户收到有关新价格的消息.

休息/阿贾克斯

  1. 客户端设置轮询间隔
  2. 在下一次轮询间隔触发时,客户端创建到服务器的套接字连接
  3. 服务器收到打开新套接字的请求
  4. 当与服务器建立连接时,客户端向服务器发送对新定价信息的请求
  5. 服务器接收对新定价信息的请求,并发送带有新数据(如果有)的回复.
  6. 客户收到新的定价数据
  7. 客户端关闭套接字
  8. 服务器接收套接字关闭

正如您所看到的,从网络的角度来看,Rest/Ajax 调用中发生了更多的事情,因为必须为每个新调用建立一个新连接,而 webSocket 使用一个已经打开的调用.另外,在 webSocket 的情况下,服务器只在有新数据可用时向客户端发送新数据——客户端不必定期请求它.

As you can see there's a lot more going on in the Rest/Ajax call from a networking point of view because a new connection has to be established for every new call whereas the webSocket uses an already open call. In addition, in the webSocket cases, the server just sends the client new data when new data is available - the client doens't have to regularly request it.

如果定价信息不经常更改,那么 REST/Ajax 场景也会经常有无所作为"的调用,客户端请求更新,但没有新数据.webSocket 案例从来没有这种浪费的案例,因为服务器只会在新数据可用时发送.

If the pricing information doesn't change super often, the REST/Ajax scenario will also frequently have "do-nothing" calls where the client requests an update, but there is no new data. The webSocket case never has that wasteful case since the server just sends new data when it is available.

这篇关于用于实时数据的 websocket 与 rest API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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