comminication when app is going background or closed


I made a web based chat application with angular.js and’s working well with web and I want to impelement this app for Ionic. I started , made something. working well with Ionic but I stucked at what happens when user close the app. I mean,user can kill the app but sender should send a message to this user.When receiver close the app, connection is killed, by the way that session is killed.When receiver open the app again,receiver create new session but sender is in still old session.So everything messing up from this moment. How can I solve this problem ? Or anyone done with any solution with this problem ? (BTW sorry for my broken English)


1 Like


First, this is has nothing to do with ionic actually, it would be the same if you were using on a web app.

You can’t trust native ids to achieve what you want.

One solution is to identify your users with a single unique permanent id (like a database id or a username).
In this case, it means both your users should join a channel matching their own id (server-side).
Let say we both connect to a chat session, I would join “shaoner” channel and you would join “fatihayyildiz” channel.
So now each time I send a message to you, it should be sent to the “fatihayyildiz” channel and vice versa your messages to me should be sent to the “shaoner” channel.
This solves the problem about disconnection / reconnection.

Now about sending messages when the other user is offline, instead of sending them to his channel, you will have to store the messages somehow (in a database). When the user goes online, he’ll be able to get the offline messages by fetching the database.

Hope this helps.

Hi shaoner,

The main problem is in this scenario, when application closed, remove that channel. I mean,I am using for send and receive message,then create room for each conversation (contains to people,receiver and sender Id’s) and I am stored this conversation in MongoDB. So I am grouping conversation with socketId,but if socket id changes everytime,conversation grouping and creating conversation methods will be imcompatible.

Another thing I am creating tabs for each conversation,If conversation name will be change,receiver and also sender will get lots of tab for each message because socketId will change every time.

One more thing,if the user is offline and other user is trying to send message,you are saying store that message in DB,right ? Ok,I store that message in db but what about socketId, I stored that message to DB with for ex. 21s34G322 socketId and receiver close app. When receiver open the app again,receiver socketId will be for ex. 3163fds6774. So Ok I will load previous message but what about receiver send a message to this session. Now socketId is different and receiver is a anonymous. My system is based on anonymous chat. One side (for ex Receiver) has username for ex. fatihayyildiz, the other side (Sender) is anonymous. So anonymous doesn’t have username and I am using socketId instead of anonymous username. So now system is turning on like this: One side username is changing every sesssion ! That where I am stucked.

Thanks for help shaoner

That’s exactly what I’m saying. The socket id changes, so you have to use another id. Use the mongodb Object Id that references each user. This is a unique id that is not supposed to change.
By default, each server-side socket joins the channel. So make your sockets join the matching mongodb id when it connects.