A practical summary of IM on the Android platform

Source: Internet
Author: User

Preface

Im communication in the development of the Internet has now been the code of the world's people know the technology, especially in the rapid development of mobile Internet in the era of the development of this technology is also more fiery, in which the veteran representative has QQ and MSN, and the recent rise of the new, silent, easy to believe, The use of IM technology is the application of a dazzling variety of applications. I am an Android developer, the main reason for writing this article is because I am engaged in the development of the main has done several of the apps are included in the IM communication, in the process of continuous problem solving, accumulated some experience records will be recorded in the blog as their own a periodic summary, can also share other needs of the developer, as a reference to the practice of the program, of course, I am not God of the gods and horses, if there are errors in the blog or after reading the reader feel that there is a doubt, please leave your suggestions or questions below the article as a comment.

from socket to XMPP protocola simple IM communication process, as shown in the 1-2-3-4 steps in the following model, the sending and receiving messages between two mobile devices require the server to relay and do the forwarding of messages.

Throughout the IM process, the server receives a request from the sender of the message and checks to see if the receiver is currently online, and if it is online, sends a message directly to the receiver if it is not in the line and stores it as an offline message in the database, waiting for the receiver to send the message when it goes online.
Im as a network communication technology, inevitably involves the problem of communication protocols, and on the Android platform to do IM development, currently I am involved in the protocol has socket and XMPP two types, of course, the two are not a side-by-side relationship, In strict sense, the socket is the cornerstone of all Java network communication, all network protocol Communication in Java, and ultimately rely on the socket to implement, on the Android platform using the XMPP protocol communication, the end of the bottom is also dependent on the socket implementation, The most typical reference example is smack, estimated that the majority of small enterprises in the development of IM, will use the XMPP protocol has been packaged smack to save development time, smack itself is developed in the Java language, and adopted the Mina as the core implementation of the framework (Mina itself is a Java NIO communication framework, but a long time not updated, the original Mina author wrote a new NIO communication framework called Netty, should be as a substitute for Mina). The main advantages and disadvantages of IM communication as socket and XMPP protocol are as follows:


Therefore, in the actual IM development, in the end is to choose the XMPP protocol and the use of sockets to achieve, according to the development of the application scenario and their merits and demerits to decide, of course, not only limited to IM development, the server to the client to do message push can also be used as a reference.

long connections and data dropsIn addition to basic messaging and the choice of protocol usage, IM development also needs to address the two common problems faced by all IM communication technologies.
One is to maintain a long connection between the client and the server. This is due to the business scenario needs, the basis of IM communication implementation is to keep the client on the server at all times the online state, so as to ensure that the sender of the message can be timely to the receiver, especially after the design concept, IM type of client has no online and offline state differences, This is the biggest difference with QQ, in this case to the client real-time online requirements become higher. So how does a client stay online in real time? Since the bottom of the Java network communication is to rely on the socket to implement, and the socket is divided into UDP and TCP two types, theoretically speaking, we use the TCP type socket to achieve communication can ensure long connection implementation, but this is only theoretical, Why alone TCP does not, this and our real network environment and operating system have a certain relationship, predicted as Mao, please continue to look down.
First of all, in the simple IM communication diagram on the previous side, only the server and client of IM communication are listed, but in the actual network environment is very complex (as shown, the yellow lightning figure represents a connection), we send the message packet is required to go through a lot of different carrier lines, Shuttle between the switches and routers to eventually reach the receiver's device, is a simple network message-passing diagram, the Yellow lightning line is connected to the long connection of TCP is maintained, theoretically this link has been maintained unobstructed, so the client only need to establish a connection can be washed away.


Second, the client sends the data drop problem, in the first reason to the operator and a variety of network conditions will lead to send data drops, caught in the following scenario A and B also experienced 1-2-3-4 of a messaging process, but unfortunately for a variety of reasons, b The message returned was lost when the IM server forwarded to a, at which point a did not know that B replied to his message, and the server did not know that a did not receive a reply from B, in which case the user experience was poor.


So how do you deal with this problem of packet loss? Now think of a simple and crude feasible solution for each time the server sends a message to the client to make an ID tag for this message, the client will need to return a receipt to the server after receiving this message, as a token that the current client has received a message. If the server sends out a message and delays receiving a receipt from the server, the message is stored as an offline message in the database, and the offline message is pushed to the client when the client reproduces the line. This way, we can solve the problem that the message packets are dropped while the client and the server are unaware. The experience suddenly on the Puma up, a ka ka.

Optimize Performanceas the real-time requirements of IM communication is relatively high, along with the long connection of the resources is more expensive, so on the mobile platform IM communication clients want performance and experience to go, performance optimization is essential, to this for Android platform IM optimization do some small summary, Of course not necessarily very comprehensive, if you have better suggestions welcome in the comments inside do add ha.


SummaryThe above content only at ordinary times in the development process accumulated part of the knowledge and experience of the summary, I hope he can help others in the search for information developers, but also to thank many contributors to open source projects and love to share the blog writer, because they have their contributions and share the birth of this article, only I in the development process of progress. The Word document of this article will also be synced to my GitHub warehouse, Welcome to make bricks, welcome reprint, but please specify the source, do not use for commercial purposes .

A practical summary of IM on the Android platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.