https://kotlinlang.org logo
Title
r

Rick Prata

03/10/2019, 4:55 AM
I'm struggling to understand how I can make my ktor server communicate with another server. Maybe because that's a new concept to me since I'm an android developer but as far as I was able to understand I would need to have a reverse proxy component. What I'm trying to do is to have periodic data fetching of another server from ktor so when my client need I will have a lot of data available that was fetched in advance. Like a workmanager on android Could anyone give me a direction and tell me where I should be looking at?
g

gildor

03/10/2019, 6:04 AM
why do you need reverse proxy?
just make HTTP request (or any other protocol that supported by other server) to another server
what is exactly you problem? do you need some way to schedule fetching? Particular implementation depends on your case, but simplest way if it just scheduled by time, so easiest implementation would be just a coroutine that you start on server start that fetch data and delay to particular time
but there is a bunch of way to do some task periodically and everything depends on your use case Like maybe you can just fetch data on start of your server and update it on next request if data is outdated
r

Rick Prata

03/10/2019, 7:04 AM
Yeah. I need to fetch data from another server every 2 minutes for each unique account I have in my database. The recurrent part I was thinking either coroutines or a scheduledThreadPoolExecutor. My issue is mostly how to actually do a HTTP request from a server to another. I tried to use HttpClient() passing the engine I'm using (Apache). It kind of worked but I am not sure if that's the best solution. Would this be the solution you are suggesting? Is there a better way of doing this? Also, as far as scalability goes, how can I keep my architecture good enough to fire the scheduled tasks every 2 minutes for each account I have on the database? Any suggestion? Thanks for the help. I appreciate it.
g

gildor

03/10/2019, 7:19 AM
I tried to use HttpClient() passing the engine I’m using (Apache). It kind of worked but I am not sure if that’s the best solution
Why? What is your concern? This is exactly use case of http client
Also, as far as scalability goes, how can I keep my architecture good enough to fire the scheduled tasks every 2 minutes for each account I have on the database? Any suggestion?
it really depends on your use case and what is your strategy in terms of data invalidation and API that remote server provides, request each profile separately may be a problem, batch request of data sounds like more scalable approacch
r

Rick Prata

03/10/2019, 6:17 PM
My only concern is that I'm new to ktor and I don't know if there is a better or proper way of doing so. I thought the way I was doing was a "hacky" way of doing it.
Butch request of data?
m

mp

03/10/2019, 10:56 PM
Batch.
You could use a scheduled executor service, or embed quartz, or have an external process that runs via cron or similar (airflow?), depending on your needs
g

gildor

03/10/2019, 11:24 PM
Sorry, batch
I would just add that cron is one of possible solutions, but using it with permanently running service looks unnecessary, using any kind of scheduling inside of your server process is just easier and more flexible imo, but it depends on your case
I'm new to ktor and I don't know if there is a better or proper way of doing so.
I don't think that this is something ktor specific, you just use Ktor http client, but even in case of Ktor Server you can use any http client implementation, you are not limited by Ktor Http client.