当前位置:网站首页>A beginner's entry is enough: develop mobile IM from zero

A beginner's entry is enough: develop mobile IM from zero

2022-06-26 05:05:00 JackJiang-

One 、 Preface

IM Up to now , It is one of the most important forms of Internet application , Especially in the mobile Internet era , It is reducing the cost of communication and the cost of communication , It has a profound impact on various application forms .

As IM Developers may soon become IM The developer's technicians ,IM The value and importance of . But in terms of technical implementation ,IM System development ( Especially the mobile end IM) There are still many technical difficulties and pits . And that's why , Quality IM Develop relevant materials 、 Practical achievements , For novices who don't have much technical reserves , Especially difficult to obtain .

This article will guide you to read relevant articles from a novice's perspective , In order to develop a mobile terminal from zero IM Be prepared for all aspects of knowledge : Including but not limited to the basics of network programming 、 Selection of communication protocol 、IM And so on . Writing co., LTD. , If there is something wrong, please criticize and correct it , I hope it works for you .( This article is published synchronously in :http://www.52im.net/thread-464-1-1.html

Instant messaging development communication : 215891622 [ recommend ]

Two 、 After reading this article  

2.1 You will get

This article will assume that you are a novice with no technical preparation , Guide you through a selection of articles , Learn how to start developing a mobile terminal from scratch IM The various technologies required 、 Materials and practical codes .
 

2.2 You cannot get

Whereas IM The complexity of Technology ,IM Development related technologies can not be demonstrated in an article , For space reasons, this article will not contain any practical code 、 Also try not to make an in-depth development of a certain technology , Relevant practical code 、 Information 、 Please read and learn in depth one by one according to the articles prepared by the author of this article , And this is exactly the purpose of this article .

3、 ... and 、 Digression

In the last two years IM The development of cloud services , Many teams, for a variety of reasons , Directly selected the short, smooth and fast cloud IM Access APP in . However , Considering the cloud IM Whether in terms of business model or operation model , After years of sedimentation , Only then can we truly realize the win-win situation of the virtuous circle of operation and service between customers and service providers . Cause rule , How to select cloud IM Service provider , This is a headache , However, this is not the focus of this article , if necessary , You can also join the discussion group mentioned in this article , Communicate with everyone : 215891622

Okay , Here's the main content .

Four 、 Network programming theory preparation

4.1 UDP、TCP The theory of agreement

We all know ,IM The business essence of the system is the real-time transmission of messages between the client and the client , The technical foundation is based on Socket Connected real-time data read / write , So the basic network programming theory is the knowledge that you must master as a novice . Of course , As IM development , Basic network theory is enough , There is no need to be as proficient as a network engineer OSI Seven layer reference model .

If you don't know what is UDP、TCP agreement , Please read the following article :

These articles are helpful to UDP、TCP The Agreement establishes a basic understanding , Of course, if time permits , Be able to read the classics of network programming theory 《TCP/IP Detailed explanation volume 1: agreement 》 It can't be better . in addition ,UDP、TCP As the basic computer data transmission protocol , Many application layer protocols have been derived from it , Relevant protocol family diagrams can be found in this article :《 Computer network communication protocol diagram ( Chinese Collection Edition )》, It can be used as your daily reference manual .
 

4.2  In depth understanding of TCP Transfer protocol

Have a thorough understanding of TCP The connection and disconnection process of the transport protocol is very helpful for you in the future IM Algorithm optimization and implementation , This process is vividly summarized as “3 Shake hands with 4 Second wave ”.

The following article will help you understand :

4.3 In depth understanding of UDP Transfer protocol

comparison TCP agreement ,UDP The data transmission protocol is very lightweight and easy to understand ,UDP It is usually used in data transmission scenarios that require rapid response , Corresponding to IM The application forms of :P2P signal communication 、 Real time audio and video, etc . in addition , Usual IM Will be used on the Internet ( Not LAN ), So understand the so-called NAT Principles of routing technology, etc , It will also help you to P2P Hole digging 、UDP There is a clear understanding of the concept of port aging .

The following article will help you develop IM It provides some practical basis for the practical application of :

Of course , Current network programming , To solve the problem of high performance , There are many shaped Socket The application layer pattern exists , such as :NIO、AIO etc. , article 《Java New generation network programming model AIO Principle and Linux System AIO Introduce 》 This paper briefly introduces the traditional blocking mode IO、NIO, The latest AIO technology , If you have time, it is necessary for you to know .( More articles of the same kind : Click here to enter …

5、 ... and 、 Basic practice of network programming

If you have read the articles on the previous level carefully , It's time to write some code , To integrate theory with practice Socket The principle and practice of communication .

of TCP Of Socket signal communication Demo Articles and code :

Of course , So I was just looking for it Demo Code , On the Internet TCP Demonstrative code for data communication is easy to find , There are no more examples here .

The author of this article has specially written the cross mobile platform UDP Socket signal communication Demo:

6、 ... and 、IM It should be used in the end UDP still TCP agreement ?

Okay , After mastering the above network programming foundation , It's time to start working for your IM Select the transmission protocol . Speaking of IM This use UDP still TCP As a transport protocol , This is a controversial topic , Every time this problem occurs in major communities, it must be a large number of different voices .

Of course ,UDP and TCP Each has its own application scenario , As IM Come on , In the early IM Because the server-side resources ( Server hardware 、 Network bandwidth, etc ) It is expensive and there is no better way to share the performance load , So many times I will consider using UDP, This is mainly early QQ As a representative .

today ,TCP The server-side load of has a good solution , In addition, the cost of server resources has decreased , At present a lot of IM、 Message push solutions are also being used TCP As a transport layer protocol . however ,UDP Nor is it excluded from IM、 Message push solutions , such as : Weak network communication ( Including transnational high latency network environment )、 Internet of things communications 、IM In real-time audio and video communication, etc ,UDP Still a preference .

The following article may help you to choose the transport layer protocol :

Of course , About IM It's time to choose UDP still TCP, It's a matter of different opinions , There is no need to be too tangled , Please start from your IM Overall application scenario 、 Development costs 、 Comprehensive consideration of deployment and operation costs , I believe I can find the answer you want .

7、 ... and 、IM Data communication format selection based on

IM In the early stage of application and development, when selecting the technology , Selection of data communication format , In the eyes of peers , Yes, it is also a very controversial topic .

Analyze carefully , The reason is , Probably lies in the following points :

  • There are a variety of protocols or encapsulation formats to choose from :
    There is a wide range of options :XMPP、Protobuf、JSON、 private 2 Base number 、MQTT、 Freeze frame XML、Plain text wait ;
  • The same format is not applicable to most scenarios :
    Different scenarios have the same considerations, and the choice of protocol is often linked to this , Such as : Mobile IM Or push XMPP When the agreement , In most cases, it will be sprayed ;
  • Developers have their own preferences for the selected format :
    Some people or teams have different experience and technical accumulation in one or several formats , It also contributes to their preference for one or more agreements .

What data communication format should be selected , It is also related to your application scenario and architecture scheme . however , At present, according to the information available to the author , As a device that needs to run on a mobile device IM, It is not recommended in almost all current mainstream discussions XMPP agreement , The specific reasons are not here , The following recommended articles will explain the reasons in detail .

The following article will be helpful to your IM Data communication format selection is helpful :

( More articles of the same kind : Click here to view the …

8、 ... and 、 Mobile IM Keep your heartbeat alive and push background messages

8.1 Why do I need to keep my heart beating ?

Due to the complexity of mobile networks , The heartbeat is kept alive for the mobile terminal IM It's especially important , In addition, the mobile phone saves electricity 、 Design of traffic saving strategy , How to keep the heartbeat alive is also very important , article 《 be based on TCP The mobile end of the protocol IM We still need a heartbeat mechanism 》 Maybe it can answer your question .
 

8.2 iOS Background message push on the client

because iOS The particularity of the platform ,iOS Once the app goes back to the background , The application itself can't use code to keep the network alive , You can't push the background messages by yourself .

The following article will help you understand iOS The background message push principle of the platform :

8.3 Android The heartbeat of the client is kept alive and the background message is pushed

Whereas Android The differentiation and incompatibility of the platforms are well known ,Android End IM When processing heartbeat preservation and background message push , Encountered a lot of trouble . and , because Android Application lifecycle management is controlled by the system , So how to ensure your IM The process or background service is not killed by the system , It is the basis for realizing heartbeat keeping alive and background message pushing .

The following article is for your Android End IM It provides a reference for the design of heartbeat keeping alive and background push scheme :

( More articles of the same kind : This entry …

Nine 、 Mobile IM Architecture design of the system

IM Its essence is a set of message sending and delivery system , Or a network communication system , It comes down to two words : Store and forward . But a mature mobile terminal IM If the system is to work properly , The content involved is far more than that , The most challenging technical skill is the design and implementation of the server architecture .

Never IM People with experience in system development , May scoff at the above view , Here I borrow TeamTalk A passage from the designer of :“IM Server development , From the perspective of functional abstraction, it can be very simple , It can be thought of as managing a large number of client connections and passing messages between different clients , But the implementation details are more complicated . Make an inappropriate metaphor ,OS The functional abstraction of is also very simple , It is nothing more than inter process scheduling and hardware resource management , But if you want to achieve one , Ordinary people can only ha ha .”

Let's take a typical scheme as an example , First, let's refine one IM The main requirements of the system : Including account number 、 Relational chain 、 Online status display 、 Message interaction ( Text 、 picture 、 voice )、 Real time video phone .......

Deal with the above requirements , We usually need to consider the following aspects to design a suitable architecture :

  • If a reliable transmission protocol is used TCP, Load issues need to be considered : The short connection realizes the account number 、 Relationship chain related business , Long connection to realize online 、 Information push ;
  • Flexibility of the background architecture 、 Extensibility : Support distributed deployment —— Put the network layer 、 Business logic layer 、 Data layer separation , The network layer and the service layer support load balancing strategies 、 The data layer supports distributed storage ;
  • client SDK Ease of use : Put the network layer 、 Data layer separation 、 Separation of business logic layer .

in addition , A typical IM System architecture design , There are also the following hot performance issues that need designers' attention :

  • Coding angle : Adopt an efficient network model , Threading model ,I/O Processing model , Reasonable database design and optimization of operation statements ;
  • Vertical expansion : Improve the performance by increasing the hardware resources or network resources of a single server ;
  • Horizontal expansion : The load is shared through reasonable architecture design and load balancing strategy in operation and maintenance , Improve performance ; You can even consider adding a data cache layer later , breakthrough IO bottleneck ;
  • High availability of the system : Prevent a single point of failure ;
  • In the architecture design, the business processing and data are separated , Thus, depending on the distributed deployment, the system availability can be guaranteed in case of single point of failure .
  • For key independent nodes, dual machine hot standby technology can be used for switching .
  • The security of database data can be solved through the redundant configuration of disk array and the primary and standby databases .

Given the limited space , The content of architecture design will not be expanded in this paper .

The following article will be for your mobile terminal IM The architecture design of the system brings certain reference significance :

( More articles of the same kind :  Click here to enter …

Ten 、 Mobile IM Communication security

IM( Especially mobile terminal IM) Security has always been a basic issue that developers need to give priority to , How to correctly understand and use encryption technology is particularly important .IM Most systems use C/S、B/S、P2P And other technologies to realize the function of instant messaging , There is no unified standard for software programming , bring IM There are many security vulnerabilities in the system itself , Coupled with the lack of security awareness of users , It leads to various security problems when using instant messaging system .

The main functions of computer cryptography today are : encryption ( Encryption)、 authentication (Authentication), Identification of (Identification) .

encryption : Prevent bad people from accessing your data . 
authentication : Prevent bad people from modifying your data and you don't find it . 
authentication : Prevent bad people from passing off your identity .

These basic concepts and encryption algorithm principles are not described here .

The following article may help you design a secure mobile terminal IM System :

( More articles of the same kind : Click here to enter …

11、 ... and 、 of IM Real time audio and video technology in

IM Real time audio and video technology in application , Almost IM The last high wall in development . The reason lies in : Real time audio and video technology = Audio and video processing technology + Network transmission technology Horizontal technology application aggregation of , And the public Internet is not designed for real-time communication . The implementation of real-time audio and video technology mainly includes : Collection of audio and video 、 code 、 Network transmission 、 decode 、 Play and other links . So many technology applications are not simple , If not , Will encounter one pit after another in the actual development process .

The following article will help you understand IM All aspects of real-time audio and video development :

( More articles of the same kind :  Click here to enter …

Twelve 、 Mobile IM Other hot issues of development

Mobile IM During the development, we will also encounter contents not mentioned above , The following article may be useful for you :
Mobile IM The technical problems that development needs to face
Development IM Is it better to use byte stream or character stream to design your own protocol ?
Do you know the mainstream way of voice message chat ?
IM How to ensure the reliable delivery of messages in the system ( namely QoS Mechanism )
Talk about mobile IM Optimization of login request in development
It's completely self-developed IM How to design “ Failure to retry ” Mechanism ?
Technical test and analysis of the influence of wechat on the network ( Paper full text )
The principle of instant messaging system 、 Technology and Application ( Technical papers )
Open source IM engineering “ Mushroom street TeamTalk” The status quo of : An open source show with no end
>>  More articles of the same kind …… 

appendix : Other IM articles

[1] of WEB End instant messaging development :
Novice entry post : In the history of the most complete Web Detailed explanation of the principle of end instant messaging technology
Web End instant messaging technology inventory : Short polling 、Comet、Websocket、SSE
SSE Technical details : A brand new HTML5 Server push event technology
Comet Technical details : be based on HTTP Long connection Web End real-time communication technology
WebSocket Detailed explanation ( One ): A preliminary understanding WebSocket technology
socket.io A little practice and thinking of message push
>>  More articles of the same kind ……

[2] Articles on Push Technology :
iOS The push service of APNs Detailed explanation : Design thinking 、 Technical principles and defects, etc
Android End message push summary : Realization principle 、 The heartbeat keeps alive 、 Problems encountered, etc
Literacy stickers : know MQTT Communication protocol
One is based on MQTT Integrity of communication protocol Android push Demo
Ask for advice android Message push :GCM、XMPP、MQTT The advantages and disadvantages of the three schemes
Mobile real-time message push technology analysis
Literacy stickers : Talking about iOS and Android The principle and difference of background real-time message push
Absolutely dry : be based on Netty Push service technology points to achieve massive access
Mobile IM practice : Google News push service (GCM) Research ( From wechat )
Why wechat 、QQ In this way IM Tools not used GCM Service push message ?
>>  More articles of the same kind ……

[3] More instant messaging technology good article classification :
http://www.52im.net/forum.php?mod=collection&op=all

( This article is published synchronously in :http://www.52im.net/thread-464-1-1.html

author :Jack Jiang ( Click the author name to enter Github) 
Source :http://www.52im.net/space-uid-1.html 
communication : Welcome to the instant messaging development exchange group  215891622 
Discuss :http://www.52im.net/ 
Jack Jiang At the same time 【 original Java Swing Appearance engineering BeautyEye】 and 【 Lightweight mobile instant messaging framework MobileIMSDK】 The author of , You can go to download and exchange .

原网站

版权声明
本文为[JackJiang-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180507139516.html