Francis Haart
Francis Haart

Reputation: 5343

How to timestamp a response consistently with an associated request?

I'm implementing instant search. Since a network doesn't preserve the order of the requests in the order of the responses, I wouldn't want a(n earlier) search for "francis" to overwrite a (later) search for "francis haart".

I was thinking of a timestamping a custom http header in the request, then extracting it and weaving it back in the response. (I'm using ajax for the requests, and django for the responses.)

What is the best practice for timestamping a response (e.g. the search results) consistently with the request (e.g. the search query)?

Upvotes: 1

Views: 142

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 14949

Easiest way (for me) would be to add a randomly generated id to the request, which will be both stored as a JS variable in the browser and also sent straight back from the server along with the results. If the response id is equal to the stored id, display it. If not, it means that another request has since been made and you can discard the one that just came back.

There's a slight risk that as the user types, intermediate result sets won't ever be displayed as the next letter will be entered before the last request comes back, but this may actually work in your favour as constantly flickering results can look messy.

Upvotes: 1

Related Questions