My delete function emits socket.io to update the other party's items list and remove the specific item. The issue arises when I receive the socket data as I struggle to find the matching item to update it.
Logic
- User 1 deletes a message
- User 2 receives a notification saying "this message was deleted"
Flow
User 1
deletes the message (works)- The message is successfully deleted for
User 1
(works) socket.io
is informed of the deletion (works)User 2
receives the deleted message data fromsocket.io
(works)socket.io
attempts to find and replace the deleted message (not successful)
Code
User 1 deletes the message
async presentPopover(ev: any, indexOfelement: any, own: any) {
// send clicked item data to popover (popover will delete it and send back results)
const popover = await this.popoverController.create({
component: MessagesComponent,
cssClass: 'my-custom-class',
event: ev,
componentProps: { id: ev, index: indexOfelement, from: 'pchat', own },
translucent: true
});
// returned results from the popover
popover.onDidDismiss().then((result: object) => {
console.log('res2021 1: ', result);
console.log('this.room.messages 2: ', this.room.messages);
if (result['data']) {
this.socket.emit('messageDelete', { message: result['data'] }); // data is "id: 187, index: 15"
this.room.messages.splice(result['data'].index, 1); // remove message from user 1 list (preferably replace the text here instead of solely removing it)
}
});
return await popover.present();
}
(popover)
// delete message (in the popover)
Pdelete(event) {
this.privateChatsService.deleteMessage(this.navParams.get('id')).subscribe((res: any) => {
this.popoverController.dismiss({ id: this.navParams.get('id'), index: this.navParams.get('index') }); //send back the data
});
}
User 2 receives an update about the deleted message
ngOnInit() {
// remove the deleted message
this.socket.fromEvent('messDel').subscribe((message: any) => {
console.log('from del emit in private chat: ', message); // data is "id: 187, index: 15"
this.room.messages.splice(message.msg.message.index, 1); // make changes in the deleted item ("index" cannot be found)
});
// end of socket.io
}
Note: The issue lies in the
this.room.messages.splice(message.msg.message.index, 1);
code where the socket struggles to locate the correct item based on the index whereas the rest works smoothly.