med.b
med.b

Reputation: 475

Execute service function in background each 4 minutes in angular 6

I'm trying to execute a service function in the background that gets the refresh token from the server each 4 minutes.

 @Injectable()
 export class RefreshTokenService {
 time: any;
 token: string;
 email: string;
 credentials: any;

 constructor(private http: HttpClient) {
 }


  getToken(): string {
   const credentials = localStorage.getItem('credentials');
   if (credentials) {
     const parsedCredentials = JSON.parse(credentials);
     const token = parsedCredentials.token;
     return token;
   }
   return null;
  }

 refreshToken(token: string) {
   const tokenJson: any = {'token': token};
   this.http.post('/api-token-refresh/', tokenJson,   httpOptions).subscribe(response => {
     const data = JSON.parse(JSON.stringify(response));
     this.credentials = JSON.parse(localStorage.getItem('credentials'));
     this.credentials.token = data.token;
     localStorage.setItem('credentials',  JSON.stringify(this.credentials));
   });
 }}

Now i'm executing the refreshToken() in the app.component.ts inside ngOnInit() , so it executes every time i load a page in the app, and it works as expected, but i haven't found a proper way to call the refreshToken() each 4 minutes in the background.

i already looked at this but it seem not to work... How to call function every 2 mins in angular2?

Upvotes: 0

Views: 3451

Answers (3)

Dmitriy Kavraiskyi
Dmitriy Kavraiskyi

Reputation: 319

Be aware of requests that are sent right after refresh token request with old valid token. I recommend to use interceptor and queue all requests sent after refreshToken. Wait until refreshToken comes and then send queue requests.

Another way - catch 401 Error -> copy request -> refreshToken -> send copy of request.

Upvotes: 1

Vikash Dahiya
Vikash Dahiya

Reputation: 5801

The best way is to use interval and switchMap operator.

First, return an observable from the refreshToken function. Use map operator to do all other stuff like transforming data, storing in localstorage etc in refreshToken() method.


import { interval, pipe } from 'rxjs'; 
import { map, switchMap } from 'rxjs/operators';

.....

refreshToken(token: string) {
   const tokenJson: any = {'token': token};
   return this.http.post('/api-token-refresh/', tokenJson,   httpOptions);
};

checkingTokenOnInterval(){
     const intervalTime = 4*60*1000;
     const token = 'sdfsdf';
     interval(intervalTime).pipe(switchMap(() => this.refreshToken(token))).subscribe(()=>{
                 //do something 
     });
}

Use checkingTokenOnInterval() for calling refreshToken call on every 4 minutes.

Upvotes: 1

skdonthi
skdonthi

Reputation: 1442

You can achieve it using RxJS/timer https://rxjs-dev.firebaseapp.com/api/index/function/timer

    this.refreshTokenService.refreshToken()
        .pipe
            tap(() => {
                // every 4 minutes refresh the discovery service
                const interval = 4 * 60 * 1000;
                timer(interval, interval).subscribe(() => {
                    this.refresh().subscribe();
                });
            })
        )
        .toPromise();

Upvotes: 0

Related Questions