Hello
I am creating chat app in ionic2.1. I am using socket.io-client and node server. The problem is that when a message received in the providers/socket-service.ts file in the following code:
this.socket.on("message", (msg) => {
/**** STEP: 3,receiveing message from node server ****/
alert(msg); // I can see message in the alert
this.socketObserver.next({ category: 'message', message: msg }); // This line is not invoking event
});
above code is expecting to raise event in the following code from pages/home.ts file:
this.socket.socketService.subscribe(event => {
console.log('message received from server... ', event);
alert("home.ts constructor" + event.category);
if (event.category === 'message'){
this.zone.run(() => {
this.messages.push(event.message);
this.chat.scrollTo(0, 99999, 0);
});
}
});
The constructor of providers/socket-service.ts is as follows:
constructor() {
alert(“socket-service.ts constructor”);
this.socketService = Observable.create(observer => {
this.socketObserver = observer;
//alert(this.socketObserver);
});
}
How much I get succeeded in creating basic chat app is as follows:
-
When user open chat app in android mobile device or in browser(via ionic serve), I can see message “User Connected” on the node terminal.
-
User type message the chat textbox and hit “Send Message” button, socket successfully emit the message through command [this.socket.emit(‘message’, message);]
-
In the socket.on method I can see that message received in the following block:
this.socket.on(“message”, (msg) => {
/**** STEP: 3,receiveing message from node server ****/
//alert(this.socketObserver); // I can see message in the alert
this.socketObserver.next({ category: 'message', message: msg });
});
In the point 3. above following line of code is not working for me:
this.socketObserver.next({ category: 'message', message: msg });
This line is expected to trigger event in the following block:
this.socket.socketService.subscribe(event => {
console.log('message received from server… ', event);
alert(event.category);
if (event.category === ‘message’){
this.zone.run(() => {
// alert(event.message); // I can’t see this alert
this.messages.push(event.message);
this.chat.scrollTo(0, 99999, 0);
});
}
});
Is there anyone who can help me with this??
This issue is resolved. The problem was very small that I was subscribing observable twice. So that was causing to lost the object.