Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a plain group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the thrust notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Ultimately the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the completes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Trio. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and suitable act will be taken.

I hope the above information gave enough skill over the app. Now we can budge forward and commence building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the adequate eclipse release depending upon your eclipse flavour)

Three. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Implements

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Display View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below directions in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Trio. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take adequate act considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have finished the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very elementary. The finish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Eventually create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Three. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a elementary group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the shove notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Eventually the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the completes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Trio. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and suitable act will be taken.

I hope the above information gave enough skill over the app. Now we can stir forward and begin building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the suitable eclipse release depending upon your eclipse flavour)

Trio. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Instruments

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Demonstrate View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below directions in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Trio. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take suitable act considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have finished the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very elementary. The finish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Eventually create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Three. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a elementary group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the shove notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Ultimately the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the finishes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Three. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and adequate activity will be taken.

I hope the above information gave enough skill over the app. Now we can budge forward and embark building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the adequate eclipse release depending upon your eclipse flavour)

Three. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Contraptions

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Demonstrate View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below directions in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Trio. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take suitable act considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have finished the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very elementary. The accomplish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Ultimately create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Trio. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a plain group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the thrust notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Eventually the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the completes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Trio. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and suitable activity will be taken.

I hope the above information gave enough skill over the app. Now we can budge forward and commence building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the adequate eclipse release depending upon your eclipse flavour)

Three. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Devices

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Showcase View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below directives in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Three. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take suitable activity considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have finished the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very elementary. The finish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Ultimately create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Trio. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a ordinary group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the shove notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Eventually the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the finishes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Trio. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and suitable activity will be taken.

I hope the above information gave enough skill over the app. Now we can stir forward and commence building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the adequate eclipse release depending upon your eclipse flavour)

Trio. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Contraptions

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Display View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below instructions in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Three. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take suitable activity considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have finished the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very elementary. The accomplish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Ultimately create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Three. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Android Building Group Talk App using Sockets – Part one – AndroidHive

Android Building Group Talk App using Sockets – Part one

We have seen a large number of applications come up in the latest past, to help us connect with each other across different mediums, like Hike, Whatsapp, Viber etc. You would be astonished to learn that its rather fairly effortless to develop one yourself. I thought providing an insight into developing such an application would be helpful for you guys.

Today we are going to learn how to build a elementary group talk app using sockets. I won't say this is the only way to build a talk app, but it is the quick & easiest way to build one. The best and efficient way would be using the shove notifications instead of sockets.

Overall we are going to build three components in this article. The very first and significant component is the socket server. The socket server plays a major role like treating the socket client connections, passing the messages inbetween clients. 2nd component is the web app where you can join the talk conversation from a browser. Eventually the android app. The main advantage of this app is, you can talk inbetween web – web, web – android or android – android.

As this article seems pretty much lengthy, I am dividing the tutorial into two parts. In this very first part, all the basic setup and building the web app is covered. In the 2nd Part, building the actual android app is covered.

Below are the final outcomes from this tutorial.

How the App Works Over Sockets?

If you are coming across the ‘sockets' for the very first time, the wikipedia page give you basic skill about socket communication. Below you can find a brief info about how our app works.

1. Very first we'll have a socket server running. When the android app or web app connects to socket server, the server opens a TCP connection inbetween server and client. The server is capable of opening concurrent connections when there are numerous clients.

Two. When a socket connection is established few callback methods like onOpen, onMessage, onExit will be triggered on the both the finishes (on server side and client side). There will be another method available to send message from client to server, or vice versa.

Trio. JSON strings will be exchanged inbetween server and client as a communication medium. Each JSON contains a knot called flag to identify the purpose of JSON message. Below is example of JSON when a client joined the conversation that contains the client name, session id and number of people online.

Four. Whenever a JSON message received on client side, the JSON will be parsed and adequate activity will be taken.

I hope the above information gave enough skill over the app. Now we can stir forward and commence building one by one component.

1. Eclipse adding J2EE & Tomcat seven Support

The eclipse IDE that comes along with android SDK, doesn't have J2EE and Tomcat server support. So we have to add J2EE and tomcat extensions. Another option would be downloading another eclipse that supports J2EE, but I would like use the eclipse that supports both android and j2ee instead of two different IDEs.

1. Download apache tomcat seven from tomcat website. (You can download it from this direct link). Once downloaded, extract it in some location.

Two. In Eclipse go to Help ⇒ Install Fresh Software. Click on Work with drop down and select Juno – http://download.eclipse.org/releases/juno. (You might need to select the suitable eclipse release depending upon your eclipse flavour)

Three. Expand Web, XML, Java EE and OSGi Enterprise Development and select below extensions and proceed with installation.

> Eclipse Java EE Developer Instruments

> JST Server Adapters Extensions

Four. Once the extensions are installed, Eclipse will prompt you to restart. When the eclipse re-opened, we need to create a server very first. Goto Windows ⇒ Display View ⇒ Server ⇒ Servers.

Five. In servers tab, click on fresh server wizard and select Apache ⇒ Tomcat v7.0 Server. Give server name, browse and select the tomcat home directory which we downloaded previously.

Check out the below movie if you not very clear with the above steps.

Two. Finding Your PC IP Address

As we need to test this app on numerous devices (it can be a mobile, PC or a laptop) in a wifi network, we need to know the IP address of the PC where the socket server project running. So instead of using localhost, we need to use the ip address in the url. In order to get the ip address of your machine, execute below instructions in terminal.

Note: The ip address of your machine might switches whenever you disconnected from wifi or a fresh device added to wifi network. So make sure that you are using the correct ip address every time you test the app.

Once the Eclipse Tomcat setup is ready and you know the IP address, you are good to go with socket server development. Building the socket server is very effortless. The socket server we are going to build won't take more than two class files.

Three. Building the Socket Server

1. In Eclipse create a fresh Dynamic Web Project by navigating to File ⇒ Fresh ⇒ Other ⇒ Web ⇒ Dynamic Web Project. Give the project name and select the Target runtime as Tomcat 7. I gave my project name as WebMobileGroupChatServer.

Once the project is created, it contains below directory structure.

Two. Right click on src ⇒ Fresh ⇒ Package and give the package name. I gave my package name as info.androidhive.webmobilegroupchat.

Four. Create a fresh class named JSONUtils.java under project's src package folder. This class contains methods to generate JSON strings those are required to have the communication b/w socket server and clients.

In the below code, if you observer each json contains flag knot which tell the clients the purpose of JSON message. On the client side we have to take adequate activity considering the flag value.

Basically the flag contains four values.

self = This JSON contains the session information of that particular client. This will be the very first json a client receives when it opens a sockets connection.

fresh = This JSON broadcasted to every client informing about the fresh client that is connected to socket server.

message = This contains the message sent by a client to server. Hence it will broadcasted to every client.

exit = The JSON informs every client about the client that is disconnected from the socket server.

Five. Create another class named SocketServer.java and add the below code. This is the where we implement actual socket server.

This class mainly contains four callback methods.

onOpen() – This method is called when a fresh socket client connects.

onMessage() – This method is called when a fresh message received from the client.

onClose() – This method is called when a socket client disconnected from the server.

sendMessageToAll() – This method is used to broadcast a message to all socket clients.

With this we have ended the socket server part. Now quickly we can build a web app to the test the socket server. Again building the web app is very ordinary. The accomplish web app can be built using basic web technologies like HTML, CSS & jQuery.

Four. Building The Web App (HTML, CSS & jQuery)

To create the web app, we don't have to create another project. This is the part of same socket server project, so go after the below steps in the same project.

1. Create a file named style.css under WebContent ⇒ WEB-INF folder. This contains the css styles for the web UI.

Two. Create another file named main.js and add below javascript. This file contains all the methods required to treat communication inbetween socket server and client. The other things like parsing JSON, appending messages to talk list also taken care in the same file.

Four. Ultimately create another file named index.html and add below code.

Five. Now run the project by Right Click on project ⇒ Run As ⇒ Run on Server. You can see the project running on http://localhost:8080/WebMobileGroupChatServer/

Five. Testing The Socket Server (using the web app)

In order to test the socket server using the web app, go after below steps. You can use numerous devices (like desktop PC, Laptop) or just one machine is enough.

1. Make sure that all the machines connected to same wifi router if you are testing the app on numerous machines. If you are testing the app using a single computer, you don't have to connect to a wifi network.

Two. Find the IP address of the machine on which socket server project is running. (Go after the steps mentioned in 2nd point to get the ip address)

Three. Substitute the ip address in main.js with your machine's ip address.

Four. Now access your project url in browser. Substitute localhost with your machine ip address in the url. My project url is http://192.168.0.104:8080/WebMobileGroupChatServer/. Access same url in another browser software or another machine's browser to talk with different machines.

Once you are able to talk inbetween numerous clients, we can go forward and build the android app in Android Building Group Talk App using Sockets – Part Two

Related video:

Leave a Reply

Your email address will not be published. Required fields are marked *