I have delete function which will emit socket.io then socket update other party items list and remove this item (replace static text) the problem is when I receive socket data I cannot find match item to update it.
Logic
- User 1 delete message
- User 2 get (static text) such as
this message was deleted
Flow
User 1
delete message (works)- Message gets deleted for
User 1
(works) socket.io
gets inform of this delete (works)User 2
gets deleted message data fromsocket.io
(works)socket.io
find and replace deleted message (not work)
Code
User 1 delete 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 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 (prefered if replace the text as well here instead of removing it)
}
});
return await popover.present();
}
(popover)
// delete message (in 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 data
});
}
User 2 gets update about deleted message
ngOnInit() {
// remove 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 deleted item ("index" cannot be found)
});
// end of socket.io
}
Note: Problem is in this.room.messages.splice(message.msg.message.index, 1);
code where socket cannot find correct item based on the index the rest is working just fine.