I have done implemention of RXJS websocket RXJS Websockets for my angular application. It is used to show the notifications on the homepage which is route /home. So once the user logins in, he receives the notifications in a mat card and a count in a mat badge using angular material lib, until this everything is fine. Now if I route to another page say for example /games, I still receive my WS messages which is fine. But now if I revisit home page /home I don't see the new WS message which I had received being at /games route, it shows the old count. This is problem one. The second problem is now if I try to send any new live message to my angular application it listens to it properly, also updates the variable which is notificationList in this case but doesn't sync with HTML view.It shows the count which we had when we logged in. Below is the snippet of my code:
Here is the HTML part which contains count and notification list:
<button mat-button #menuTrigger="matMenuTrigger" [matMenuTriggerFor]="notifications"
<mat-icon [matBadge]="counter" [matBadgeHidden]="counter < 1" matBadgeColor="warn">notifications</mat-icon>
</button>
<mat-menu #notifications="matMenu">
<ul>
<li *ngFor="let item of notificationList; let i = index">
</li>
</ul>
</mat-menu>
Here is the TS part which contains RXJS websocket code a short snippet of my code:
import { webSocket } from 'rxjs/webSocket';
public retryConfig = 3000;
public myWebSocket: WebSocketSubject<any>;
this.url = 'ws://localhost:8081';
fetchNotificationData() {
this.myWebSocket = webSocket(this.url);
this.myWebSocket.pipe(
retry(this.retryConfig)).subscribe(
(dataResponse: any) => {
this.counter = this.dataResponse.length;
this.notificationList = this.dataResponse;
},
// Called whenever there is a message from the server
(err: any) => console.error("CONNECTION FAILED::", JSON.stringify(err)),
// Called if WebSocket API signals some kind of error
() => console.log('complete')
// Called when connection is closed (for whatever reason)
);
}
Any help or advice will be appreciated. Thanks a ton.