Skip to content
Advertisement

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. 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)

$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.

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?

Advertisement

Answer

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

For example a Redis cache (or similar technology) is one option if you don’t want to use a database or other persistent storage.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement