简体   繁体   English

WebSockets 将第三方 API 流式传输到多个子客户端

[英]WebSockets streaming third party API to multiple subclients

We have a data-driven application where our backend continuously fetches data from a third party server and then feeds it over websocket to our clients frontend.我们有一个数据驱动的应用程序,我们的后端不断从第三方服务器获取数据,然后通过 websocket 将其提供给我们的客户端前端。 Now the issue is that whenever each client will come the same data has to be fetched twice for both of them which is deemed unnecessary.现在的问题是,每当每个客户端来时,都必须为它们两个都获取相同的数据两次,这被认为是不必要的。 Suppose the following code (in PHP for example)假设以下代码(以 PHP 为例)

$apiData = file_get_contents('https://third-party-api.com/feed/data.json');
$sockets->send($apiData);

Now our client on frontend can request this data via websockets.But the issue is if the second client connects, then the same data is downloaded twice.现在我们前端的客户端可以通过 websockets 请求这些数据。但是问题是如果第二个客户端连接,那么相同的数据会被下载两次。

Note: We do not want to save data (no database or anything at all because its of no use to save data).注意:我们不想保存数据(没有数据库或任何东西,因为它对保存数据没有用处)。

Is there a method where if two clients are connected then the same feed can be sent to both of them?有没有一种方法,如果连接了两个客户端,那么可以将相同的提要发送给他们两个?

If you want to send the data from the API it to two clients who are not both connected at the time the data is fetched then you'll need to cache the data somewhere如果您想将数据从 API 发送到两个在获取数据时都没有连接的客户端,那么您需要在某处缓存数据

For example a Redis cache (or similar technology) is one option if you don't want to use a database or other persistent storage.例如,如果您不想使用数据库或其他持久存储,Redis 缓存(或类似技术)是一种选择。

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

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