Android Chat Application using GCM (Google Cloud Messaging) – Server

In this series of tutorials we are going to see how to develop a basic Android Chat Application with GCM (Google Cloud Messaging) , Node.js server and MongoDB database.

What we will do ?

-> Login User and store data temporarily in MongoDB database.
-> Upstream Messaging through Node.js server.
-> Downstream Messaging using GCM.
-> Shows user who are logged in.
-> Notification displayed when new message arrived.
-> Remove data from database when user logged out.
-> Persistance with Shared Preferences.

Our tutorial has two major parts

1. Setting up server with Node.js, MongoDB, GCM.
2. Develop client side Android Application for chatting.

Setting up Server Side

Registering you Application with GCM (Google Cloud Messaging)

1. Open Google Developer Console .
2. Create a new Project.
Android Chat Application using GCM
3. Select Overview and note down the Project number.

Android Chat Application using GCM
4. In the left pane under APIs & auth select APIs and enable Google Cloud Messaging for Android.
5. Then select Credentials section.
6. Create a new Key select type as Server key. Enter the IP of your server. If you are testing in localhost enter the IP as 0.0.0.0/0.

Android Chat Application using GCM
7. Then Note down the API Key.
Android Chat Application using GCM

Prerequisites

See the following tutorial to setup Node.js and MongoDB in your Linux or Windows machines.

How to Setup Node.js and MongoDB in Ubuntu and Windows

Use the GUI client Robomongo to access MongoDB. It is available for all the platforms.

Download Complete Project

 

Node-Android-Chat
Node-Android-Chat
Node-Android-Chat.zip
Version: 1.0
13.4 MiB
9132 Downloads
Details

Creating the project

-> Create a MongoDB database named node-android-chat using Robomongo.

Android Chat Application using GCM
-> Create a directory named node-chat and change to the directory.
-> Create package.json file and include the following dependencies.

package.json

{
  "name": "node-chat",
  "version": "0.0.1",
  "main": "app.js",
  "dependencies": {
    "express" : "~4.0.0",
    "mongoose" : "~3.8.8",
    "connect": "~2.14.4",
    "request": "2.36.0"
  }
}

 

-> Now enter npm install in Linux terminal or windows command prompt.
-> Now all the packages begins downloading and it is available in the directory node_modules.

Lets see why we use these packages.

express:

express is a Node.js framework. It is used to simplify some of the basic tasks.

mongoose:

mongoose is a Node.js module which is used to interact easily with MongoDB databases.

connect:

Connect is a express middleware which is used to parse form data, and to use logger which prints the request in the terminal.

request:

Request is used to sent Requests such as POST from our Node.js server to GCM server.

Creating app.js

Create a file named app.js in the root of the node-android folder. It is the main file which makes our server up and run. Here our application runs on the port 8080 in localhost.

app.js

/**
 * Module dependencies.
 */
var express  = require('express');
var connect = require('connect');
var app      = express();
var port     = process.env.PORT || 8080;
// Configuration
app.use(express.static(__dirname + '/public'));
app.use(connect.logger('dev'));
app.use(connect.json());
app.use(connect.urlencoded());
// Routes
require('./routes/routes.js')(app);
app.listen(port);
console.log('The App runs on port ' + port);

 

If you wish to receive new articles related to Android Chat Application using GCM (Google Cloud Messaging) – Server #1 enter your email address in the field below and subscribe:

Advertisements

One comment on “Android Chat Application using GCM (Google Cloud Messaging) – Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s