Chat algorithm with websockets and gcm - android

I have a chat web site and I builded an app of this site.Now my site and app using socket.io(websocket).In current version of my app when user closes the app user can't recevie any message(normally).I decided add GCM support to my app for push notifications.But I have questions about this.
My scenario:
User 1 logged into system from pc then he connected to socket.io
server and he is waiting messages.
User 2 logged into system from android app then he connected to
socket.io server.
Now they are chatting with each other.
But 10 minutes later user 2 closed the app and now he won't receive messages.
I am asking what should I do for send push notifications to user 2 ?
I am not asking GCM implemention,I know the GCM implemention.I know the user 2 device id and how can I send push notification to this user.
My problem is:I need to work with socket.io and GCM.How can I detect "okay,user 2 closed the app when user 1 sends a message to user 2 take this message and send to gcm server"
Is this a good approach ? or what do you suggest for like this chat system ? Example you can give an example from Whatsapp how can they handle this problem ? I think they are using Gcm for only push notifications,in app they are using Erlang.

It's not socket.io that connects to the GCM.
You'll need a module for Node.js that enables the connection from the Node.js (running the socket.io) to the GCM. A module like this one gcm module, there are also examples that can get you started.
So, in your case, you'd want to call socket.emit() if the user is online (you should have this already), otherwise the user is offline and you want to push the message by calling gcm.send().
To detect if the user is offline you'd need the to add the user to an offline list when the socket.on('disconnect',function(){//add user to offline list}) handler is called.

Related

Android: Developing a Chat app

I am developing an one-to-one chat app. The problem is how should I keep all clients and server in sync? There can be multiple cases when the message fails to send from the client, or it is not received by the client.
Right now, I am using a pub sub service and GCM. Whenever user is using the app, I subscribe to a unique channel to receive messages and use api to send message. Also, through pubsub I am maintaining user online status.
Whenever app goes into background, pubsub disconnects and user goes offline. In this case GCM is used to deliver messages to the client.
This systems is working fine. But rarely there are messages that are not delivered to the client.
Can this be improved to assure message delivery ?
Here is a good tutorial.
with client side and server side codes...

server update device not in use

I am developing a small chat application using node.js for iOS and Android, Now my question is that one user created a account and logged-in using this application from iOS or Android, then the user deleted this application without log-out. How the server knows this device has deleted the application without log-out?. Since, while user again installing this application, the push notification is still arriving without the user log-in.
Apple has a feedback service to give you information about failed remote notifications. Basically you listen the messages from feedback.push.apple.com and you flag the devices returned from feedback service. So no further push notifications will be delivered to that device. When user installs your app again you can remove the flag and start delivering the notifications.
For the android part check this question; Is there an Android GCM equivalent to the iOS Push Notification Feedback Service?

GCM between User to User from same Android App

I would like to implement GCM in my Android app:
One Android app
Registered- U_user and another M_user.
I have Google Sign In in my app thats works fine with JSON and GSON and local MySQL database server with tomcat-server restutil to keep storing Gmail login users.
So, I want to send a notification from U_user to M_user.
U_user does a registration that has been pushed to M_user.
How can I do this?
I am assuming you mean the same Android app installed on different phones (U_user's phone and a separate phone for M_user).
To quote a section in the docs:
To make sure that messages go to the intended user:
Your app server can maintain a mapping between the current user and the registration ID.
The app can then check to ensure that messages it receives match the logged in user.
What this means is that you can send messages to another user through GCM. To let the app server know what to send to M_user, let U_user send an upstream message to your GCM app server (only possible in app servers using XMPP - see here). Alternatively, you have the option to do POST requests to your app server via HttpUrlConnection (can work on both XMPP- and HTTP-based GCM app servers).
Regardless of which method you use, specify in these messages to the server that you would like to send a message to M_user.

Using socket.io with gcm

I have a chat website.I am using socket.io in my web site.I need to do an android app for this site.I will use socket.io in app for real time conversation but also I want to add push notification support with gcm.I know the gcm but I need a roadmap.
This is my algorithm:
(In this algorithm,user 1 sends a message to user 2 from web and user 2 uses android app)
1)Get the user 1's message to server
2)Send a ping to user 2 via socket.io
3)If user 2 replies this ping that means user 2 using app right now,send message with socket.io
4)If user 2 doesn't reply this ping that means user 2 was closed the app,server sends a http request to gcm server for send a push notification to user 2.
Is this a good approach ? If you reply is "no" how should be ?

Implementing one to one and group chat in android [duplicate]

This question already has answers here:
How to make a chat application in android? [closed]
(6 answers)
Closed 9 years ago.
I am developing an Android app in which I have to implement chat messaging. I would like one to one chat or a group chat.
But I have no idea how to start. Please help me with this stuff. Any help will be appreciated.
A simple chat mechanism will have 2 basic functionalities
Send the message to server (with info about the recipient)
Receive the message from server (designated for my user name)
First step is simple, we can create a web service which will accept the message with additional information about recipient(s). We can create it using any server side language.
Step 2, that is fetching the message from server can be done using 2 techniques, Pull the message (using polling) from server, or Push the message from server to android phone
Polling: In this, the android device will keep accessing server after a few seconds to check if there is a message available for user. This again can be implemented using a simple async task at the client side which will keep calling a web service after say 2-3 seconds. This is fine to use if we are planning to enable chatting only when user is accessing the app (no notifications like gmail or facebook), so that we can kill the polling service when not in use (otherwise it will eat up resources).
Push notifications: a better option is to use push notifications. Android provide Google cloud messaging or GCM (http://developer.android.com/google/gcm/index.html) which will help achieve push from server easily. Otherwise you can try a third party API like urbanairship or pushwoosh depending on your requirement. Push notifications will help the user to receive messages even when he is not using the app.
So in nutshell, a webservice to receive the messages and a push notification mechanism should be sufficient to implement a chat service in android.
Little bit about UrbanAirship
I used UA in one of my projects for push notifications as I needed to support both iOS and Android. If you just want to support Android GCM might also be a good option.
Coming back to UA, check this for sample code and usage: https://docs.urbanairship.com/display/DOCS/Home
The way it works is simple, when someone installs the app and is connected to internet, app registers itself to the UA service. A unique code is specified for each installed app (this is the time when you can capture the user name and unique code and store somewhere in your DB). Next UA provides an API using which you can push a message to designated recipient(s), using the unique codes which are available with UA. These messages can be received by android app and used as per the requirement. Even if the app is not running we can show a notification just like when we receive an email or a message
You can use an existing platform like Scringo. It gives you a one-on-one chat as well as group chat (both the client and the server) as well as the push notification service.

Categories

Resources