why use beacon api when it does not provide a response - javascript

I am using navigator.sendBeacon() api, I understand its benefits of sending data even onunload, but why should we use it when it only returns a value of whether the data is being queued or not and does not help us in checking if the data is sent to the server side successfully.
Could someone help me in understanding this and provide a solution to check if the data has been sent to the server successfully.

The point of sendBeacon is to send data when you can't wait for a response anyway (such as when the browser is leaving your site and you want to send analytics data to your database).
There isn't a way to get a success or fail state from it, because it is designed for situations where your JS program is exiting (and can't block the exit).

Related

How can i listen for http requests on a client side

The logic of my code is basic.
The user sends a request to the server side, where it is processed and shown in an admin panel. Afterwards a person with access to the admin panel analyses the data and sends a response with some delay.
How can I create a response listener on the client side, so that I can catch the message I get from back-end, no matter the delay?
I tried doing it with fetch, but no wonder it didn't work, because once it is compiled, it makes the action immediately. Is AJAX an option in my case?
You'll need to have some sort of bidirectional communication layer here. The most common approaches are polling, web hooks, or sockets. Polling will probably be the easiest to set up in a beginner use-case.
If you're referring to jQuery's $.ajax, which uses XMLHttpRequest, it's not likely to be a good idea unless the server can respond very quickly every time. From what I understand, if the request isn't fulfilled within a reasonably short period of time, the browser or OS will terminate it. Fetch might have different limitations, but I still wouldn't trust it for something like this.
There are better approaches. Either create a websocket, so that the server can push information to the client on demand, or (less elegant) have the client repeatedly make requests to the server (say, every minute) and have the server respond positively if/when the admin panel has been dealt with.

How to determine when an API job is completed?

Please excuse any misconceptions I may have, I'm new to HTTP requests.
I am using a file conversion API (online-convert.com) to convert an uploaded MIDI file to MP3.
The conversion process involves 3 HTTP requests that must be completed serially, I'm using Fetch for this.
The 2nd HTTP request uploads the input file to the server, and the 3rd HTTP request gets the completed job details. The issue is, if I try to make the 3rd request as soon as the 2nd request is completed, then the response states that the conversion process is still underway and the output is not ready.
My naive solution was to set up an intervalic loop that continues to make this 3rd request every x milliseconds, until the response indicates that the job is complete. This feels like an unnatural solution, and I was wondering if there's a better way to do this.
Sorry if my question is specific to the API I'm using, but any advice would be appreciated!
Thanks to Patrick Evans' comments above.
I have decided to get the completed job's details by polling as opposed to by callback URL.
I was able to implement this easily using Fetch and async/await, and found this to be easier than setting up server side scripts for a simple web tool that probably did not require them.

Synchronous http interface with javascript

I have a synchronous API to invoke a server (HTTP GET) currently implemented with XMLHttpRequest.
The API does caching and will, if cache isn't deemed too old, return from the cache and invoke the server asynchronous to refresh cache.
Sometimes cache isn't available or too old, and then the API will synchronous call the server to fetch an accurate value before returning result to caller.
Result will contain a boolean success flag along with payload and clients handles result accordingly by looking at this flag.
There are two problems I can see with doing like this;
When cache isn't available and server isn't reachable or answering slow I would like to bring up a spinner so that the user is aware we are waiting for server.
In addition I would like to set a timeout value where we abort server request and handle the error accordingly.
Seems like I should be able to use setTimout operations but I have not been successful.
Preferably I would like to keep clients intact (not change the API to asynchronous).
Is there a way to achieve this?
The synchronous API was made responsive by maintaining a cache that was pulled from server asynchronous.
The cache was protected by a grace period under which we do not pull new value from server to avoid hammering the server.
For the most cases this was enough to assert there was always a cached value that could be provided to the client.
For a few cases where we have to pull new data the best solution would be to go fully asynchronous, that is also update client code.
Currently that is not an option, so in addition to above a heartbeat mechanism was put in place that toggles online/offline status to prevent trying synchronous pulls when offline.

Event based Ajax communication between server and browser

I'm going to write an application, having some worker threads on the server, and some log and status elements on the html page. logs and status are expected to be updated whenever an update is ready from the server side.
well, one approach is to set up a polling mechanism, like the client sends a request on specified intervals and the server sends back the last update, (if any available).
however I wonder if there is any more efficient way like an interrupt-driven approach, on which whenever an update is ready on the server a message is sent to the client through an Ajax call. and as long as no update exists no message is transferred back and forth.
first of all, is this possible to initiate a call from the server side? I mean via Ajax.
or is there any library like JQuery that facilitates such a requirement?
Thanks
Consider using web sockets (Available in HTML5) - This will allow you to skip polling an update the data immediately as the server sends up his finish request.
Read more on:
http://www.html5rocks.com/en/tutorials/websockets/basics/

Can JavaScript be used to send and store information on a server?

For example: When you ask a question on stackoverflow, you input information into the text box, and that information is sent to the server where it is stored and displayed to the end-user.
Can the process of sending this information, and retrieving/displaying it be written in JS?
If yes, is it a good language to do so or are there more efficient ones?
If no, what is this process usually written in?
Thank you in advance.
Yes. You can use AJAX (http://en.wikipedia.org/wiki/XMLHttpRequest) to send and receive information from a server.
you can use ajax calls to do any server side methods. Please check the link
[edit removed irrelevant response after re-reading original question...]
As far as javascript's role, it's called a client-side language because it really just lives in the client's browser, and not on the server. It is a very nifty tool for keeping an eye on what the user is doing in the browser, packaging up data, and firing it off to send to the server. That's when the server-side languages would take over, process the data, and send a response back to the client's browser. Usually, the client's browser receives the server responses, but javascript is also capable of receiving server responses, which is usually called AJAX (Asynchronous JavaScript and XML).
It's a very broad subject, but I appreciate that you have to start somewhere to know what more specific questions to ask. Hope this helps.

Categories