Phoenix channel - requesting multiple APIs and asynchronously pushing results to client

I have an ordinary HTML website and backend server phoenix. Both HTML and Phoenix run in a different server.

My HTML site display price from different sites. From my HTML site, I will connect to Phoenix Server using the Phoenix channel.

When the form is submitted on my HTML site. It will send an event through the phoenix channel.

On the server-side, I need to fetch the prices for a particular item from 15 different websites. I will run all these 15 API calls in a Task Module.

Now what i want is each API call response time differs.

I want to push the price asynchronously through the Phoenix channel as the response comes to the client.

So on the client-side, I can display price as I receive. Instead of making the client wait until I receive all the prices.

can someone give me insight on how to achieve the asynchronous push on the Phoenix channel from the server-side to the client?

Your help is greatly appreciated. Thanks

If you want to get multiple responses for a push you likely need to manually do what phoenix does for the single response case automatically:

Create some unique key, pass it with the push to the server. Setup a generic listener on messages from the server. The server when having computed any of the results must then broadcast a general message including the key of the initial request. The generic listener can then match the received response back to the request.

You also want to have the client be in a channel nobody else is connected, unless many clients shall receive the responses.

2 Likes