Synchronizing data on the same web page across different browsers with Angular 8

I have developed an Angular application where I am fetching data using Angular HttpClient to consume a REST API. The received data consists of values that need to be displayed on a form page. When I make edits to the data and click save, the changes are saved through HttpClient with a PUT call to the REST API.

Being new to Angular, my concern is when I open the same page in two different browsers and make changes in one browser, I want those modifications to be reflected in the other browser as well. How can I achieve this in Angular 8?

The calls to the REST API are handled in a separate service defined as:

getData(): Observable<IData> {
   return this.http.get(`${this.serverurl}`);
}

updateData(data: IData): Observable<IData> {
   return this.http.put<IData>(`${this.serverurl}/save`, data);
}

The service calls are implemented as follows:

ngOnInit() {
  this.myHttpService.getData().subscribe(
    data => {
      this.myData = Object.assign({}, data);
    }
  );
}

onSaveClick() {
  if (this.myData) {
    this.myHttpService.updateData(this.myData).subscribe(
      response => {
        this.myData = response;
      }
    );
  }
}

What additional steps do I need to take to synchronize data across multiple browsers?

Answer №1

After some experimentation, I came up with a solution that involved utilizing the Interval function and nesting my HTTP request within it:

ngOnInit() {
  interval(1000).pipe(
  takeWhile(() => this.alive)).subscribe(() => {
    this.myHttpService.getData().subscribe(
      data => {
        this.myData = Object.assign({}, data);
      }
    );
   });
  }

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

Executing multiple queries in a node.js transaction with Sequelize using an array

I am looking to include the updates on the clothingModel inside a transaction, with the condition that if it successfully commits, then update the reservationModel. This is the snippet of code I am attempting to refactor using sequelize.transaction tr ...

Working with relative paths in React Native TypeScript using WebStorm

My variable color is located in the path app/theme. To set it up, I created a package.json file in app/package.json with the following content: { "name": "app" } Now, to import color in TypeScript files, I use the following syntax: import { color } fro ...

Guide on integrating a Single Page Application into an ASP.NET Core library and hosting it from a specific route

Scenario I am working on creating an aspnetcore library or module that will contain a small SPA frontend. The goal is to package the html/js/css files along with the dll, and serve the SPA from a specific path (/some-module) without the need for configura ...

Error in Typescript: _this2.onClick is not a callable function

For react-native development, I rely on typescript. In my project, List and ListItem components come from NativeBase, with ListItem resembling a TouchableOpacity. ... public onClick = () => this.props.navigation.navigate("NextScreen"); public _renderRo ...

Separate HTTP responses into multiple Observables

I need assistance in splitting a HTTP response into different Variables/Observables. The returned data is in JSON format: [ { "status": [ { "id": 1, "value": "active"}, { "id": 2, "value": "inactive"} ] }, { "type": [ ...

Definition file in TypeScript for an npm package provided by an external source - constructor

In my Node project, I am utilizing ES6 and Typescript. Despite this, there is a commonjs library that I need to incorporate. To address this, I have created my own .d.ts declaration file for the library: module "@alpacahq/alpaca-trade-api" { e ...

Tips for implementing Conditional validation in form models in anugular2

I need to determine whether the required validator for Address should be applied based on the value of this.type being 2. Below is the code snippet I am using for form validation: buildForm() { this.orgForm = this.fb.group({ Name: [this.addUpd ...

Learning how to implement GraphQL in Angular without relying on the Apollo client library poses an exciting challenge for

Exploring ways to incorporate GraphQL into my Angular app without relying on the Apollo client. Any suggestions on how this can be achieved? Although I've used the Apollo client for connecting to a GraphQL API in the past, I'm curious about alte ...

Activate the code when any of the observables changes

I have a few observables within my state service that are generated from a behavior subject: _state.sortModels$.subscribe(sortModels => { //do something }); _state.filterModel$.subscribe(filterModel => { //do something }); I am looking to ...

Tips for Managing the Hardware Back Button in Ionic 3

Can someone help me enable the hardware back button in Ionic 3? I want it to redirect to specific pages and display designated content. Being new to Ionic 3, I need guidance on how to set up the hardware device buttons for redirection. ...

Oops! There was an unexpected error in the authGuard: [object Object] was not caught as expected

I've been working on implementing authGuard in my app, but I keep encountering an error. Below is the guard implementation: canActivate(route: ActivatedRouteSnapshot): Observable<boolean> { /** * Returning an observable of type boolea ...

Styling a specific row in Angular application

In my Angular application code, I am trying to iterate through the tr elements and apply styles to specific rows. There would be multiple rows needing this treatment. For example: <tbody> <tr *ngFor="let row of fsIncomeStatementTable | ...

Image not found in next.js

Working Environment ・ next.js ・ typescript ・ styled-components I uploaded the image in the folder inside pages, but it is not showing up. Why is that? // package.json   { "name": "nextapp", "version": &qu ...

An issue has been encountered at the header component of an Angular web application, identified as error code

I recently created a small Angular web application and decided to create a Header component using the command line ng g c Header. https://i.sstatic.net/ZgCi0.pngI then proceeded to write a brief paragraph in the header.component.html file. <p>header ...

Creating a Route in Angular 2 for a Component other than the one initialized with the bootstrap function

I am currently in the process of working on a project involving Angular2. If you are interested in understanding why I need to do what I am about to explain, please take a look at this issue. The main component in my project is called AppComponent and it ...

Issues with implementing Dark mode in TailwindCSS with Nuxt.js

After spending a couple of days on this, I'm still struggling to get the dark mode working with Tailwind CSS in Nuxt.js. It seems like there might be an issue with the CSS setup rather than the TypeScript side, especially since I have a toggle that sw ...

List component in Angular not refreshing after sorting the array in the service

Currently, I am delving into the realm of Angular (version 5+) to enhance my skills by working on a small project. The project involves setting up basic routing functionalities to showcase the ContactList component upon selecting a navigation tab. Addition ...

The Angular7 counterpart of the C# attribute decorator

I'm working with an API method that has an Authorize attribute to verify permissions. [Authorize(ReadIndexes)] public async Task<IActionResult> GetIndexes () { ... } Is there a similar way in Angular to implement permission checks so the API ...

Exploring JSON data in Angular

I am currently working with a Json file that contains two different objects: readers and books. Here is an example of the structure: { "users": [{ "id": 1, "username": "peterB", }, { "id": 2, "username": "MaryC" ...

React Native component that enables users to both input their own text and select options from a dropdown menu as they type

Looking to develop a component that combines Text Input and FlatList to capture user input from both manual entry and a dropdown list. Has anyone successfully created a similar setup? I'm facing challenges in making it happen. Here's the scenari ...