Tips on accessing real-time data from a JSON file

I have been working on this project for a while now and wrote some test scripts. I am using setInterval to update the content every 500 seconds, but I am facing an issue with accessing the input fields. For example, if there is a form with input fields, I am unable to write in them as my input gets deleted automatically. The reason I am using setInterval is because I want to continuously see real-time inputs from the database.

Here is the index script:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="x in myData">
   <a href="http://php.net/manual/ro/function.json-encode.php"> {{ x.Name + ', ' + x.Country }}</a><input type="text" />
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
 setInterval(function(){$http.get("customers.json").then(function (response) {
      $scope.myData = response.data.records;
  });},500); 
});
</script>

</body>
</html>

And here is the JSON file:

{
  "records": [
    {
      "Name": "Alfreds Futterkiste",
      "City": "Berlin",
      "Country": "Germany"
    },
    {
      "Name": "Ana Trujillo Emparedados y helados",
      "City": "México D.F.",
      "Country": "Mexico"
    },
    ...
    // More records continue below
    ...
    {
      "Name": "Comércio Mineiro",
      "City": "São Paulo",
      "Country": "Brazil"
    }
  ]
}

Answer №1

If you're looking to build a real-time application, consider utilizing node.js with socket.io (websocket) for the best results. Take a look at this example of how to implement socket.io(websocket lib):

https://socket.io/get-started/chat/

Furthermore, traditional ajax calls can be slow and may take more than one second. That's why real-time web applications should use websockets instead of regular ajax requests.

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

Securing Communication with HTTPS in Express JS

After purchasing an AlphaSSL from a hosting provider, I received the following files: domain.csr.crt domain.interCert.crt domain.PKCS7Cert.crt domain.rootCert.crt domain.X509Cert.crt However, in order to enable HTTPS on Node.JS using Express, I am aware ...

Pause the concurrent.futures process upon meeting a specific condition, and resume execution once the task is completed

Utilizing the Python request library, I am extracting JSON data from a website (). However, this website requires correct cookies for data retrieval. To handle this, I use the selenium webdriver to obtain the necessary cookies before fetching data for 850 ...

What are the steps to utilize chrome.tabs.onUpdated.addListener?

I am currently working on a Chrome extension where I need to display an alert() showing the page URL whenever the user switches tabs or enters a new URL in a tab. However, my current implementation is not functioning as expected: chrome.tabs.onUpdated.ad ...

How to Retrieve Superclass Fields in Angular 5 Component

I have a superclass that provides common functionality for components. export class AbstractComponent implements OnInit { public user: User; constructor(public http: HttpClient) { } ngOnInit(): void { this.http.get<User>(& ...

Establish the starting time for the timer and use the setInterval() function according to the seconds stored within the object

How can I configure the values of timerOn, timerTime, and timerStart to make the watch start counting from 120 seconds? Currently, the clock starts counting from 3 minutes and 22 seconds, but it should start from 2 minutes. You can find all the code here: ...

Is it possible to execute a URL twice instead of just once in AngularJS?

While developing a web application with AngularJS and Rest Web services, I encountered a problem where the URL is being executed twice instead of just once. I have created a service function for executing the web service API and calling it from the control ...

What is the best way to create ng-options that only display unique values?

I am trying to modify my ng-options element so that it only displays unique values. Currently, it looks like this: https://i.sstatic.net/5CtSk.png However, I want it to display one value at a time. For example: <select class="form-control" ng-model=" ...

Symfony/encore requires devDependencies in order to successfully compile

My experience with Symfony5 and encore has been smooth until I attempted to deploy to production. In order to install dependencies, you can use the command npm install --production. To compile, run npm run build --prod. I encountered an issue when trying ...

Printing in ASP.Net without displaying a dialog box

Is there a way for my web application to automatically print a popup page without prompting the client to choose a printer? I am looking for guidance on implementing silent printing in ASP.Net using java-script or ajax, or any other suitable solution for ...

Tips for utilizing document.write() with a document rather than just plain text

Currently, I am utilizing socket.io to develop a party game that shares similarities with cards against humanity. My main concern is how to retain the players' names and scores without needing to transmit all the data to a new page whenever new games ...

Looking for a way to identify when a DOM element is resized as a result of web fonts loading asynchronously?

Within this div element lies text styled with a custom font via CSS's @font-face attribute. The issue arises when the font hasn't loaded yet, causing a delay in updating the font style of the text within the div. As a result, taking measurements ...

Does anyone have any insight on why I can't seem to remove an item from my list of tasks?

Having trouble with my React todo list. After submitting, the list item looks fine. I expect to be able to delete it by clicking on the item, but nothing happens. When I try to add another item, the page refreshes and all items are removed. The console ...

A simple guide to running Express Js and MongoDB from the command line and gracefully closing the terminal

Is there a way to run an Express project without the terminal being visible or easily closed? I need my server to stay running, but I don't want the user on the local PC to be able to see or close the terminal. Any suggestions on how to achieve this ...

Tips for sorting through various elements or items

How can I improve my filtering function to select multiple items simultaneously, such as fruits and animals, or even 3+ items? Currently, it only allows selecting one item at a time. I attempted using , but it had bugs that displayed the text incorrectly. ...

The combination of Capybara, Sweet-Alert, and click_button appears to be functioning correctly, however, it is not initiating the

Trying to automate a test here. There's a website with an element and a delete button to remove that element. Manually, everything works fine. The current test script is as follows: visit "/argumentation#!/overview" expect(page).to have_content("Phi ...

Troubleshooting problems with AngularJS placeholders on Internet Explorer 9

On my partial page, I've included a placeholder like this: <input name="name" type="text" placeholder="Enter name" ng-class="{'error':form.name.$invalid}" ng-model="Name" required /> I have also set up client side validation for the ...

Identifying the Operating System of Your Device: iOS, Android or Desktop

I am in need of displaying different app download links based on the user's operating system. This includes IOS, Android, or both if the user is accessing the page on a Desktop device. I am currently utilizing React and Next.js for this project. Unfor ...

Are the shadows in the scene being affected by a problem between DirectionalLightHelper and CameraHelper?

Currently, I am working on a complex static render in the browser using three.js and I have encountered an issue while trying to produce accurate shadows with a single THREE.DirectionalLight that represents the sun in my scene. All the geometry in another ...

The process of transforming a String response into an iterable entity

I'm currently working on an Android/Java app where I need to make a REST API call using the Volley library. Here's the code I have so far: RequestQueue queue = Volley.newRequestQueue(this); String url ="https://covid19datasl.herokuapp.co ...

Activate Bootstrap dropdown with an external button click

I am currently working with Bootstrap 5 dropdowns and I have a specific requirement. I want the button that triggers the dropdown to be located outside of its parent element (under A). Is there a way to achieve this using Jquery or JS? <div class=&quo ...