Use Http/2 to make api calls from javascript
Asked Answered
I

1

12

I know most browsers support http/2 for loading the pages but does this mean I can leverage it when I make api calls using XmlHttpRequest ?

More specifically my question is if I make 2 calls to fetch data using XmlHttpRequest does it ensure that both of them use the same tcp connection underneath ? None of the documentation I read any where specifies any thing about http2 support for XmlHttpRequest or how I can explicitly open a http2 connection, make some calls leveraging this and then close the connection.

The okHttp, jetty and other libraries in java offer client libraries to support this. But the javascript support is not clear.

Incorporeity answered 12/8, 2016 at 15:38 Comment(0)
C
16

When the browser and server both support HTTP/2 then it will be used for all calls from the browser - including XHR. You don't need to do anything special in your javascript to enable this. That's one of the great things about the way HTTP/2 was implemented.

If you are asking how to ensure the connection is held open for as you want to reuse the connection then the http/2 spec says:

HTTP/2 connections are persistent. For best performance, it is expected that clients will not close connections until it is determined that no further communication with a server is necessary (for example, when a user navigates away from a particular web page) or until the server closes the connection.

...

Servers are encouraged to maintain open connections for as long as possible but are permitted to terminate idle connections if necessary.

Cathiecathleen answered 12/8, 2016 at 18:56 Comment(10)
I don't think the browser keeps the tcp connection open forever. How does it know when to open and when to close the tcp connection ? That's the reason I suspect the support.Incorporeity
If there is any documentation that supports the theory it would help a lot. I couldn't find any though.Incorporeity
Updated the answer to include the details I think you're looking for. Though not sure how you think this is different to how this worked under http/1.1 connections with keep-alives so not sure why exactly you're asking that as a follow up to what I thought the question you were asking was?Cathiecathleen
I know what the http2 spec says and know that the browsers load the html and associated css, js resources over a http/2 leveraging a single tcp connection & push promises for low latency. What is not clear is how the browser works for direct api calls.Incorporeity
Coming to my requirement I absolutely need to ensure that a bunch of my calls use the same tcp connection so that all my requests land on the same server over a server farm(a single tcp conn ensures that). I have java clients which work this way as I explicitly control the connection. But with javascript the browser could close the connection if it detects a idle session or may be it closes the connection after a timeout. That's what I need to find out so that I can evaluate whether it is feasible to create a reliable js client that will work in a similar fashion.Incorporeity
So I suspect it is not possible in javascript to explicitly let browser know that I want the tcp connection to be help open. I will wait for other replies to see if anyone else knows any other options to achieve this in js. Otherwise I will just close this as the answer.Incorporeity
You cannot guarantee this under http/2 (nor http/1.1) as that's browser and server dependent. You cannot even control this with a Java client as the server can drop it at any time (either as connection is idle, server is busy or another reason). Main point is browsers do not handle api calls differently to how it loads normal html, css or javascript (though appreciate your point that less chance of different connections if loaded over short time). Suggest you set up your server farm to route requests consistently somehow (e.g. based on client IP address - though those can change too).Cathiecathleen
I get what you are saying as per spec. But rich clients like okhttp2 will let you know when the connection is dropped so that you can try again from the start. :). I don't need all the calls during the app lifetime to land on the same server. My requirement is to just land 2 sequential calls to land on the same server. So I open the conn and then make the 2 calls, wait for responses and then close. If the connection is terminated by server I retry.Incorporeity
For example implementation of this you can look at amazon's alexa api. It leveraged the http2 concept of single tcp connection to do something similar.Incorporeity
Haven't used Alexa api but seems they suggest pinging every 5 seconds to keep the connection alive (developer.amazon.com/public/solutions/alexa/alexa-voice-service/…). You could do the same using javascript assuming browsers use that to keep the connection alive (which is not specified in any spec other than the one I've given and could well differ between browsers). Though still no 100% guarantees which is why the section below that discusses how to handle server disconnects.Cathiecathleen

© 2022 - 2024 — McMap. All rights reserved.