Transferring data through asynchronous JavaScript and XML streaming

Is there a way to access AJAX data before the request is complete in order to achieve streaming, similar to the example below:

ajax_request.send();
interval = setInterval(function() {
    continueParsing(ajax_request.responseText);
    if (download_complete)
        clearInterval(interval);
}, 64);

Currently, I am using a PHP method to break up the request into smaller parts, but I would prefer to handle it all at once. What is the best approach to accomplish this, focusing on compatibility with Chrome and Firefox?

Answer №1

Here's a PHP snippet to get you started:

$data = file_get_contents('https://example.com/feed/');
for($x = 0; $x < 5; $x++){
   $data .= $data;
}
echo $data;

And here's a sample javascript code:

var request = new XMLHttpRequest();
request.open('get', 'ajax.php');
request.send();
request.onprogress = function(){
    console.log(this.responseText.length);              
}

When I run this, the console shows:

9441
128365740
1375294687
1324567889

It seems to be working fine. Feel free to experiment further :)

Answer №2

To handle this task effectively, it is recommended to utilize WebSockets along with a suitable backup plan for older browsers, such as AJAX long polling.

For PHP users, there is a project called http://code.google.com/p/phpwebsocket/ that can offer a straightforward solution. While it may not have built-in support for fallback technologies, integrating socket.io-client on top of it could potentially solve this issue and allow the phpwebsocket project to serve as your server layer.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Locate elements within an array based on certain internal characteristics

I have an array of objects that contain a property called "distributionChannel". I need to find objects within the array that have a value of "sellChannel" for the key in the "distributionChannel" property. Here is my array: [ { "lineItems": [ ...

Selecting objects in Three.js using the camera but without using the mouse

I am working on a Three.js app where I need to determine the object that the perspective camera is focusing on. In order to achieve this, I consulted the raycaster documentation. Most of the resources I came across discuss using raycasting with a camera an ...

A common challenge in React is aligning the button and input line on the same level

I'm currently working on a React page where I have an input field and a button. My goal is to align the bottom edge of the button with the bottom line of the input. Here's the code snippet I have: `<form className='button-container'& ...

Number each element in sequence

Looking to give sequential numbering to elements by iterating through them. For instance, if there are 6 input elements, the goal is to update their names correspondingly like "name=input1", "name=input2", and so on. This involves using a for loop to reas ...

What is the best way to immediately update the state in a React functional component?

Having recently started learning React, I find myself struggling to understand the lifecycle of a functional component. Let's consider a scenario where I have multiple checkboxes labeled as: checkbox, a, b, c, and d, each corresponding to values a, b, ...

Press the option "Switch to red button" in order to transform the text color to red, and then revert back to its initial color

I am trying to implement a feature where the "convert to red button" changes the text to red in the preview paragraph, and then reverts back to the primary color according to the theme - black in the light theme, white in the blue and black background them ...

Issue with data entry: unable to enter the letter 'S' in search field

This bug has been a real challenge for me. It's strange, but I'm unable to type the letter "S" into the search input field. Oddly enough, the keyboard seems to be functioning properly. Please find the sandbox below for reference: https://codes ...

Ensure the browser only reloads a single file

In the midst of my project, I realized that the CSS and JS files are being loaded separately onto the webpage with query strings like .css?1234 to ensure the browsers refresh the files on load. However, upon returning to the project, I discovered that one ...

How to disable typescript eslint notifications in the terminal for .js and .jsx files within a create-react-app project using VS Code

I'm currently in the process of transitioning from JavaScript to TypeScript within my create-react-app project. I am facing an issue where new ESLint TypeScript warnings are being flagged for my old .js and .jsx files, which is something I want to avo ...

Interacting with iframes using JSDOM: Manipulating div elements

Attempting to extract information from the website http://www.example.com, which contains the following HTML code: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My site</title> </head> <b ...

Looking to transform a nested JSON structure into a visually appealing HTML table with merged rows?

My JSON structure appears as follows: $scope.data = [ { "type":"Internal", "count": 3, "library" : [ { "type":"Library 123", "count": 2, "version" ...

Execute an API request function in the views.py file using AJAX

Apologies for any naive inquiries, but I'm struggling to grasp the inner workings of these relationships in Web development. In my views.py file: https://i.sstatic.net/9cekE.png You'll notice a connection to the API where the necessary informat ...

Challenges with fading images using jQuery

I am currently working on animating a 5 image slideshow by creating a fading effect between the images rather than just switching abruptly. Here is my HTML structure: <div id="slides"> <ul class="pics"> <li><img src="imag ...

What are the best practices for storing an array of objects in MongoDB with Mongoose?

For my project, I needed to store data in a mongoDB document as an array of objects using Javascript, Node JS, Express framework, and Mongoose library. After researching online, I found two different methods to achieve this: Creating another sub-schema ...

Add a variety of input values to a div in designated spots

Objective: The aim is to allow the user to connect form fields with specific locations within a content from another div, and then add the text entered in individual input fields to those corresponding locations. Here is the link to the fiddle HTML: < ...

Having trouble figuring out how to update a list using ajax in Yii

I need to modify a JavaScript function that filters the content of a list. The current code looks like this: var id = $(this).data('id'); $.fn.yiiListView.update('contests-list', { data: {category: 2} }); I couldn't find any ...

Adjust camera focus towards object and utilize the lookAt() method (React three fiber)

Seeking a smooth transition for camera.position and camera.lookAt as I switch between "zoomed out" and "zoomed in" views of individual objects randomly placed. The positioning aspect is working smoothly, but lerping the lookAt() function seems to be causi ...

Maximizing the potential of Next JS 13 Server Components

Exploring the updates in Next JS 13, I have found it intriguing that every component is now a server component by default. This concept has been puzzling for me as I try to grasp how to effectively leverage this feature. For instance, my current challenge ...

"Error Encountered When Trying to Parse JSON in Javascript Due

After retrieving JSON data from a remote URL, I encountered the following: {"myitems":[{"NAME":"JOHN"},{"NAME":"MICHAEL"},{"NAME":"CATTY"},{"NAME":"DAVID"}]} I am trying to parse this in JavaScript using: JSON.parse(mydata); However, I keep encounterin ...

When using fs.readFileSync, the error message "no such file or directory" may appear even though the file is successfully returned when using require()

Looking to dynamically read a JSON file poses a challenge with the traditional require() method. The issue lies in the fact that the file is not updated until the NodeJS server is restarted, prompting the need to utilize fs.readFile or fs.readFileSync. Wh ...