Nisha Patel
Problem
I am making a chat application using Ionic 6 and SignalR for test purposes only. I have a chat detail page, and in this page I am listening a BehaviorSubject which triggers from SignalR. Here is my working principle:
Sender sends a message
Sender triggers "CheckMessage" in SignalR
Receiver's SignalR gets it and triggers a BehaviorSubject which I've subscribed on chat detail page.
It gets the last sent message and show it on the screen.
Everything works as expected, but when I minimise the app and reopen it, after sending/receiving a new message, it shows two times at the screen. When I repeat this, this last message shows at the screen n times (n = how many times that I minimise the app).
How can I solve this issue or what am I missing here?
signalR.cs:
app.component.ts:
signalR.service.ts:
v.component.ts:
detail.component.ts:
Solution
This is happening because in ngOnInit() lifecycly of your detail.component, you get the last message every time it starts:
Solution: before doing this.messages.push(msg), (or preferably in you service.getLastMessage() function) somehow check that if the last message already exists or not, and if not, then add it; something like:
Suggested blogs:
>How can I access specific data inside a JSON file on an Angular?
>HTTP request after another but only care about first one in Angular?
>Why am I getting unexpected parent reaction to child button Angular?
>How can I combine multiple HTTP calls in Angular using RxJS?
>Why Ng serve doesn't work in Angulars?
>How to use JSON:API along with Angular/RxJs?
>Why Ng server doesn't work in Angulars?
>How to operate data before return it using Angular16?
>How to Embed Node Red into existing Angular application?
>Why Angular routing with mat-toolbar is not working?
>Update Observable with value from a different Observable in Angular
>P-Dropdown doesn't set Value manually with Reactive Form in Angular
>Why Apex stacked chart bar is not showing in the x-axis in Angular?
>How can I delete the 3rdpartylicenses.txt file in Angular?