Speed Up Real-time Communication By Incorporating Socket Programming Into Your App

0
Shares
Speed Up Real-time Communication By Incorporating Socket Programming Into Your App
5 (100%) 2 votes

incorporate-socket-programming

What passes your mind when you hear about the term ‘real-time communication’?

Of course, on-request real-time, synchronous communication between server and client.

But in the mobile app world, do you know what type of apps generally need or use real-time communication?

The answer is, any app that establish connection between with server for exchanging data. For instance, chatting apps like Whatsapp, Wechat, and Hike. In simple words, any application that sends a request to server for fetching some specific data.

You got an overview about real-time communication, but, as a developer, how can you implement it in your native Android app?

To do that, you’ll need to incorporate Socket programming into your app. In this tutorial, we will guide you how to do it.

What is Socket?

Well, a socket is one end-point of two way communication between two programs running on a network. The Socket classes are used to represent a connection between client program and server program.

Now, there are other options available to perform an exact operation, so why are we implementing it through Socket programming?

Any guess?

No?

The reason is, socket is better than ‘HTTP’.

How?

It’s because you can get fairly low latency using long-held connections for client to server and vice versa.

Also, the socket is faster than ‘HTTP’ because you won’t need to create new requests over and over again. Instead, you’ll need to just open or create a connection between your client and your server. This connection will remain open, until you close it which means both client and server can send data anytime. Therefore, sockets are standard for ‘real-time communication’.

Implementing SocketClient.java

First, we’ll create a socket class on client-side SocketClient.java.

Open your Android Studio and create a new project under file menu. Once your project is created, check the left side tab, you’ll see a couple of folder icons. From there go to apps→ src→ main→ java→your package name -> SocketClient.java. Create a class file and name it ‘SocketClient.java’.

Now, copy the below code in OnCreate() function.

/***
*IpAddress server ip address
*Port Defin socket port Like 8080,8081… etc

Socket socket = new Socket(IpAddress, Port);
//Check is Socket Connection successfully?
if (socket.isConnected()) {
Connected!
}
//Send Data to Server And get Acknowledgement
OutputStream out = socket.getOutputStream();
PrintWriter outToServer = new PrintWriter(out);
outToServer.print(“type your message here”);
outToServer.flush();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
String response = “”;
InputStream inputStream = socket.getInputStream();
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString(“UTF-8”);
}
//Close Socket
if (socket != null) {
socket.close();
}
Log.w(“log_tag”, ”acknowledgement: -” +response);

By implementing above code, one socket will be created and through that socket, client will be able to send a message and wait for the acknowledgement.

Implementing ServerSocket.java

At server-side, we’ll create one server socket with port. Then, we’ll get the message via Server socket that we sent from the client-side and send the client acknowledgement of received message.

At last, we’ll close the connection.

ServerSocket socket = new ServerSocket(Port);
//Get Socket
Socket socket = serverSocket.accept();
//get Message From Client
String messageFromClient = socket.getInputStream().readUTF();
//Send Acknowledgement to client
socket.getOutputStream().writeUTF(“Hello from Server”);
//Close Socket
if (socket != null){
socket.close();
}

Summary

Overall, pretty simple. So, by implementing both codes in your native app, you can easily speed up real-time communication between client and server.

If you’ve any queries regarding this tutorial, you can discuss it with one of our developers. We have integrated a Socket in many Android apps. You can check our portfolio here.

Also, if you’re looking to hire mobile app development company to transform your app idea into reality, you can discuss with us.

 
0
Shares
 

LET'S TALK VALIDATE YOUR IDEA!