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.
Related
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).
I am trying to implement a simple UI which shall be showing the logs written in my server console. Have searched but couldn't find a solution which satisfies my requirement.
As per my design, I have a java program using Apache common-io api for tailing log file. It helps me to reduce memory overhead, I do not want to keep large chunks in memory.
So when client makes a request, server shall start reading file and send the read data incrementally and shall keep showing until client stops receiving. I do not wish to send multiple request because that would make application read file again and again adding to which I would need to maintain a state/offset (possible solution but avoiding it).
I tried to check for JAX-RS using Asynchronous Response but that doesn't seem to help. I am not sure if HTTP/2 is gonna help.
Please help me understand how this can be achieved, and if I would need to implement socket programming at client and server side or if there is any such protocol which can be used. I am open to modify tech stack.
Thanks
You can use any protocol that supports long lasting streaming connections (which there are many).
If you're already using JAX-RS, then StreamingOutput might be what you want.
After bit of more searching I finally found a bunch of ways of achieving what I mentioned. Before I would like to explain how it cannot be achieved using HTTP (Rest specifically).
HTTP Way: There are few ways in HTTP and/or HTTP2 where you can create a long lasting connection using long-polling in both versions or using multiplexing property present in http2. In both cases the underlying protocol in TCP so there not much difference. However, in HTTP/HTTP2 transactions occur in a fashion where once server receives a request and sends response back, it doesn't expect client to receive response again neither client expects to receive one. So one complete cycle includes a pair of request and response. Hence, if you try to send another response you cannot do that because neither client nor server would be able to receive or send that respectively. There are many resources in Google for more in-depth information.This has a good explanation and references
So I tried to check if I can use some socket coding in order to keep the connection alive and transmit data. Luckily I stumbled upon another way to achieve that.
Two of which I felt make more sense for my requirement are as follows. I would not try to explain them just to avoid providing wrong information here since I myself am trying to get more insight.
1. Server-Side Events(SSE)
2. WebSockets
This will give a fare idea about them.
Am calling an ajax call to validate n number of data.Since it takes lot of time to complete, I thought of showing the user the progress bar or tell that 1/n completed In-order to display that, I should get the status from the controller.Can someone please tell me is there any way to get the status from controller before completing? Or Is there any other better way to implement.
Since ordinary AJAX is a classic request-response you cannot get progress information from the server with the same connection. You have to make a new AJAX call to ask the server for the task progress. This is called a heartbeat pooling technique.
Better solution is to use some kind of persistent connection, for example Websockets. That way server can push a message with progress information to the client over the same connection that initiated the task. Websocket adoption is strong among browsers.
XMLHttpRequest Monitoring progress:
see here: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress
more here:
How to get progress from XMLHttpRequest
looks like this is in the standard
https://www.w3.org/TR/progress-events/#firing-events-using-the-progressevent-interface-for-http
I am a bit of a newbie in Webhooks, so excuse me if this is a simple question.
I am clear about how Webhook providers work, i.e. whenever this information needing to be pushed, it sends the payload to the URL specified as callback.
Now my question is: how do I write a client-side Webhook handler, that can detect/process the callback and update my client-side accordingly. For example, if my client-side is a simple web-page with bullet-points, I would like to append new data to the list, whenever it comes through.
Preferably, I would be after a complete JavaScript solution...
Is there perhaps a JS WebHook Client/Handler that already exists? It seems that this should be so common, that it should exist, although I haven't been able to find anything.
Take a look at WebSockets. Depending on your needs, this could be exactly what you need to avoid polling and keep things in sync - particularly if you have a lots of clients who need to see the same updates from your server.
I highly recommend Socket.IO
To consume a webhook API endpoint, or in other words, to "listen for changes", you'd poll for changes, long-poll for changes, or anything else clever you'd like to do.
Or you can use any javascript Publisher Subscriber module to easily do this. try googling around for PubSub stuff. here's an example of one such tool: http://www.pubnub.com/tutorial/javascript-push-api
web hooks are not made for this. Event notification in web hooks is done through POST requests meaning your client app cannot be notified about new events unless it listens for incoming HTTP requests (usually the client is behind a firewall so this will not be feasible in most cases).
If you'd like to avoid polling the server for status updates, use WebSockets as matthewhudson pointed out.
This is a followup question to the one here
Here's briefly what I am trying to do. The File server creates a text file to indicate an end of the process. On a webpage on the Web Server, I loop every x seconds and make an ajax request to find out if the test file exists (ajax request to http://fileserver/files/UserFile.txt)
I've tried the following approaches so far:
Trigger a web method from the client side that creates a HttpContext object to verify if the text file exists. But this is too strenous on the server and I started getting all kinds of exceptions in the Event Viewer.
YQL works great but unfortunately it's too slow. The user waits atleast twice the amount of time.
I am looking for a solution that doesn't involve the server side. Somehow, I'd like to use JQuery to verify the existence of a text file on the fileserver.
Any thoughts?
You should be able to use JSONP and JQuery.ajax() to do cross-domain request work. Play with the jsonp and jsonpCallback attributes. Alternatively, you can use JQuery.getJSON().
Serving a single file from the filesystem is the most simple operation a web server can do. If that is already too much, then all other solutions will be worse. Find out why the server takes so long to serve a simple file and fix that.
Note: I'm assuming that the file is small since you say "test file". If it's a big file, the server will actually send it to the client which will need a lot of resources.
What you can try is to add an ASP page to the web site which runs code on the server that checks whether the file is there and just returns a tiny piece of HTML which you can add to the page with jQuery.load().
I may be miles off base here but... could you not create ONE asynchronous (!) Ajax client request with a HUMONGOUS timeout. Fire it, and wait. You would be invoking some server script that checks every so often, in a loop on the server (using sleep in between), whether the file exists. And not replying to the Ajax request until the file finally shows. The server script then replies and exits.
EDIT: Depending on the server-side scripting framework used, you may even get some OS support. You may be able to sleep on a status change in the directory...