B4J Question How send parallel http request without delay?

behnam_tr

Active Member
Licensed User
Longtime User
Hello
I want to send 10 HTTP requests at the same time
Time is very important to me, all requests must be sent exactly at 10:00:00
There is no serious problem when there is only one request
But when there are 10 requests, all subsequent requests are delayed by 100 to 200 milliseconds and sometimes more.
Is there an optimal solution to this problem so that the amount of delay is as low as possible??
Normally, I send as follows

B4X:
'class1
timer1.interval=1000

sub Timer1_Tick

 sendHttpRequest  'It may run 5 times with an interval of 1000 milliseconds

end sub

sub sendHttpRequest
    Dim url As String=  "https://site1.com/profile"
    Dim j As HttpJob
    j.Initialize("j",Me)
    j.Download(url)
    j.GetRequest.SetHeader("Content-Type", "atext/plain")
    j.GetRequest.SetHeader("sec-ch-ua",  $" "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"" "$)
    j.GetRequest.SetHeader("sec-ch-ua-mobile", "?0")
    j.GetRequest.SetHeader("sec-ch-ua-platform",$" "Windows" "$)
    j.GetRequest.SetHeader("Upgrade-Insecure-Requests", "1")
    j.GetRequest.SetHeader("DNT", "1")
    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
    j.GetRequest.SetHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
    j.GetRequest.SetHeader("Sec-Fetch-Site", "same-origin")
    j.GetRequest.SetHeader("Sec-Fetch-Mode", "navigate")
    j.GetRequest.SetHeader("Sec-Fetch-User", "?1")
    j.GetRequest.SetHeader("Sec-Fetch-Dest", "document")

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
   
       Log(j.GetString)
   
    End If
    j.Release
end sub
=====================================

'class2
timer1.interval=1000

sub Timer1_Tick

 sendHttpRequest  'It may run 5 times with an interval of 1000 milliseconds

end sub

sub sendHttpRequest
    'Using different data and different address
    Dim url As String=  "https://site2.com/time"
    Dim j As HttpJob
    j.Initialize("j",Me)
    j.Download(url)
    j.GetRequest.SetHeader("Content-Type", "atext/plain")
    j.GetRequest.SetHeader("sec-ch-ua",  $" "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"" "$)
    j.GetRequest.SetHeader("sec-ch-ua-mobile", "?0")
    j.GetRequest.SetHeader("sec-ch-ua-platform",$" "Windows" "$)
    j.GetRequest.SetHeader("Upgrade-Insecure-Requests", "1")
    j.GetRequest.SetHeader("DNT", "1")
    j.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
    j.GetRequest.SetHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
    j.GetRequest.SetHeader("Sec-Fetch-Site", "same-origin")
    j.GetRequest.SetHeader("Sec-Fetch-Mode", "navigate")
    j.GetRequest.SetHeader("Sec-Fetch-User", "?1")
    j.GetRequest.SetHeader("Sec-Fetch-Dest", "document")

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
   
       Log(j.GetString)
   
    End If
    j.Release
end sub


When I take the test, the time of sending the first request for the first class is 10:00:00:50
And for the second class and the first request, it is 10:00:00:150.
I want this difference of 100 milliseconds to disappear for the second class and send it exactly at the time of 10:00:00:50

If the number of requests increases, the amount of delay will definitely increase by a small amount for the next requests
 

William Lancee

Well-Known Member
Licensed User
Longtime User
If you want to send two or more request starting at the same time (for all practical purposes).

https://www.b4x.com/android/forum/threads/order-of-completion-of-multiple-async-calls.159430/

These calls are asynchronous, so there is no guarantee that they will arrive at certain times.

To start sending at a given time, make a timer activated check time sub and call the multiple requests when the time is right.
The timer interval should be as small as possible. The check time sub should be a short as possible.
 
Upvote 0
Top