D
D
Denis Shcherbina2020-06-10 07:51:03
Angular
Denis Shcherbina, 2020-06-10 07:51:03

Ionic 5: A previously opened component continues to run in the background. How to fix?

I am developing a multi-user chat on ionic 5. I use a modification with angular.js
Description: There is a service component that starts the socket. There is a page with a list of dialogs and the dialog page itself, which I go to every time, passing the user id, there is a correspondence. In the dialog page, I have a socket function to receive messages. thanks to her, I found that the component with its parameters continues to work after closing.
Let's say I opened the dialog page with different users:
/home/dialog;id_chat=1;id_friend=1
/home/dialog;id_chat=2;id_friend=2
/home/dialog;id_chat=3;id_friend=3
Now, being on the same dialog page with different parameters, for example, "/home/dialog;id_chat=7;id_friend=7", I see that all previously opened components continue to work.

Specifically, the problem is that when communicating with users id_friend=7, I read messages from those users whose dialog I opened, and the dialog can be opened 3 times, which means that somewhere in the background there are 3 identical pages that continue to work ...
How to fix it? How to kill a component in the background every time I exit it? I don't think socket is the problem, because I'm outputting console.log(id_friend), this value is different for each dialog.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Shcherbina, 2021-11-08
@Denis_maker

And so, here's the denouement: when the page is opened, socket listeners are created. They are the ones who create this problem because they continue to exist after the page is closed. When the page is opened, subsequent times listeners continue to be created, there are more of them.
Solution: close socket listeners when leaving the page. Here it is written how to do it: https://qna.habr.com/q/1006197 (there is code and justification in the comments to the question marked as the answer)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question