I'm trying to create a very simple chat application in angular using socket io. I short I have a component that views the chat and service that listen to incoming messages and adds them to the component object to be viewed.
My problem is that I'm passing the conversation object(a reference to the listener so that It can append the message to the object), however, the new message does not appear in the chat component.
Should I change the object into a behavior subject since I reckon the problem is with updating the view?
Please find the code below:
Conversation Service:
listenToIncomingFromAllConversations(conversations: Conversation[]) {
this.webSocketService.listen(this.authService.currentUserValue.id).subscribe(data => {
console.log("The message recieved from the server is:")
const message = data as Message;
var conversation: Conversation = null;
console.log(message);
this.getUserConversations().subscribe(convos => {
/** Refresh conversation and decorate them */
conversations = convos;
this.decorateMessageTabConvos(conversations, convos);
conversation = conversations.find(convo => convo._id == message.conversationID);
console.log("The conversation appear and ready to be append by the new message!");
console.log(conversation);
conversation.otherMemberID = this.getOtherMember(conversation.members);
conversation.messages.push(message);
this.decorateChatBoxMessages(conversation);
});
});
}
Chat Component:
ngOnInit(): void {
this.getConversaitonDecorated();
this.convoService.listenToIncomingFromAllConversations(this.conversations);
}
getConversaitonDecorated() {
this.convoService.getUserConversations().subscribe(convos => {
this.convoService.decorateMessageTabConvos(this.conversations, convos);
});
}
As you can see the listener listens for messages from the server and is initialized in the lifecycle hook of the chat application and the conversation object (the holds all the user's conversation) is passed to the listener for the message to be appended to it.
The message gets appended since it is passed by reference and appears in the console log. However, when I view the specific application that the message was appended to, it is not visible in the view.
Thanks