Best Practice Method to Get Screenshot Requests as Soon as Ready
Posted by Brandon Elliott on 13 August 2016 01:19 AM
Do you want to show a web page screenshot to your users the instant it is ready?
Do you want to use something like Ajax to continuously check for ready state?
Do you want to avoid being charged for constantly polling with excessive requests to ShrinkTheWeb to check for ready state?
This article addresses all of these questions. Some users have hammered this service with requests every 1-2 seconds (or even more than once per second), checking to see if a screenshot is ready. That is excessive and uses up costly resources. So, we pass those costs on to the customer in the form of bandwidth charges for each hit. However, it is not our intention to make money this way but rather to pass along costs for integrations that are not ideal. However, we prefer that users follow the "Best Practice" method below and save us AND themselves, time, money and frustration; while simultaenously improving their user's experience. ;)
Consideration #1: Security
If you are using jQuery/AJAX/etc or any "in-browser" code to poll our service, then you or your users may be facing cross-domain security issues. All of this is overcome by using a "middleman" or "proxy" script on your domain to make the initial request and provide status to your "checker" script.
Consideration #2: Keeping costs low
To avoid racking up unnecessary bandwidth charges, all integrations should rely on our "Notify Callback" feature to quickly deliver screenshots when ready and use local placeholders to designate request status. See below for an example.
Consideration #3: Getting screenshots as soon as ready
Our "Notify Callback" workers are constantly delivering or notifying users of screenshots that are ready. Typically, a "Notify" worker will notify of a ready capture in less than 1 second. Under extremely heavy load, there may be delays, so we recommend having a fallback check once every 20-30 seconds, just to be certain. This secondary check is not handled in our sample code.
Please note that if the screenshot is already READY, then we will ignore the CALLBACK request, because it is quicker, and less work on both sides, for you to just go ahead and download the image immediately. The process would be to parse the XML response of your request, every time, and if the status is "delivered" (image download URL will be provided), then go ahead and download the image. This is handled automatically in the sample code, but if making a custom integration; it is important to recognize this additional step.
Note: If this extra step, when READY, causes undue work to integrate, let us know. I am not opposed to adding an override to always push the image (in case it simplifies development).
Consideration #4: Figuring out the code
Fortunately, we went through the trouble of doing all of this ourselves, using our Snapito screenshot demo site to get a feel for creating a great user experience. We include an example script in our sample PHP code to help you get started or use as a guide for converting to another programming language. We also provide a basic jQuery example to help get you started there as well.
Related terms: callback notify notification screenshot delivery