How to develop mobile chat function from scratch based on IM SDK

The Development of IM Instant Messaging Technology

Instant messaging (Instant Messaging) is an Internet-based instant messaging business.

The real-time chat interaction function is one of the important functions of mainstream APPs on the market. People are familiar with WeChat and QQ chat messaging systems. IM seems simple, but technology development is by no means easy. Massive concurrency, ultra-low latency, and messages must Reaching such high real-time requirements requires the application integration of many technologies;

In recent years, with the in-depth penetration of the mobile Internet and the rapid development of social +, IM has spawned many new ways of playing, not only applied to social chat scenes, but also appearing in various scenes such as e-commerce, live broadcast, and customer service. widely used by people.

Research data shows that more than 60% of the apps on the market have instant messaging capabilities, and users can chat with other users directly in the app in real time, which helps to improve app activity and user experience. The realization of IM function has become a compulsory course for application developers.

The author has been engaged in IM development for more than ten years. This article mainly shares three implementation methods of IM development, hoping to be helpful to IM developers.

Three Ways to Realize IM Instant Messaging

The three implementation methods of IM development are: 1. Open source code 2. Self-developed 3. Integrated IM SDK. Developers can choose according to the project situation.

The implementation of an IM product can be roughly divided into three important parts: client development, server development, and service operation and maintenance.

  • Client development: including mobile apps, desktop software, web pages, applets, etc. on various platforms.
  • Server development: Responsible for the realization of various functions of IM, such as user access, relationship chain maintenance, sending and receiving messages, file and picture storage, security audit, etc.
  • Service operation and maintenance: A long-term operating product must require a continuous operation and maintenance process to ensure the stability and reliability of the IM server, such as the expansion of business volume. The realization of any part of the above three parts from zero to one will be quite difficult. Complete self-development requires relatively high requirements for the ability of project members and resource investment. Except for large companies that will develop completely by themselves, they generally adopt the method of citing open source codes or integrating commercial IM SDKs. The following summarizes the differences between these three implementations.

Table: IM3 implementation methods

\Implementation vs Dimensions open source code Self-study Integrate commercial IM SDK
Difficulty Low high middle
Functional scalability Relatively difficult to rely on open source project plans Does not depend on external conditions, relatively simple Rely on other manufacturers, moderate difficulty
Operation and maintenance cost Own operation and maintenance, high cost Own operation and maintenance, high cost Do not need to operate and maintain by yourself
On-line cycle middle slow quick
For people personal experience Large companies with strong research and development capabilities Small and medium companies, personal studios

The first implementation method: using open source projects to implement IM chat

The fastest way to realize the IM function is to choose an open source project, not only to stand on the shoulders of giants, but also to gather the wisdom of the whole community for rapid development;

How to choose an open source project for IM? The completeness and activity of project functions are the main dimensions for reference. Based on past experience, I have selected two pretty good open source projects for your reference.

1.MobileIMSDK

Project address: https://github.com/JackJiang2011/MobileIMSDK

MobileIMSDK is an original mobile IM communication layer framework, which is lightweight and highly refined, and has been tested for 8 years. It is the only similar open source framework on the market that supports UDP+TCP+WebSocket three protocols at the same time, supports iOS, Android, Java, H5, and the server is based on Netty.

PS: It should be noted that the H5 side of the project is not yet open source, and the applet and Uniapp are still under development.

2.OpenIM

Project address: https://github.com/OpenIMSDK/Open-IM-Server

The founding team of OpenIM comes from IM senior architects, and is developed by the IM/WebRTC expert team. It is committed to creating service value with open source technology, creating a lightweight and highly available IM architecture, which is convenient for developers to build a variety of instant messaging and real-time audio and video interactive scene.

Drawing lessons from open source projects is suitable for tight development cycles and does not require too many customized developers, which can help developers quickly realize IM functions. However, the functions are generally relatively simple and basic, and subsequent function extensions are heavily dependent on the development progress of open source projects. If the demand for customized functions is relatively high, or the future business volume is relatively large, it is recommended not to be lazy to use this method.

Teams with high requirements for IM customization and R&D capabilities generally adopt the method of self-research, and then I will share with you the technical difficulties and pitfalls in the process of self-research.

The second implementation method: self-developed IM chat

IM technology involves a wide range, and complete self-development requires relatively high requirements for R&D team capabilities and capital investment, and the R&D cycle will also be relatively long. Long-term planning is required to prevent missed business opportunities.

Figure: Overview of self-developed technologies

insert image description here

In the process of self-developing IM, we also encountered some difficult technical problems, which are listed here for your reference, such as

  • Reliability and orderliness of messages.
  • Real-time message push and message pull in high concurrency scenarios.
  • Message storage scheme selection: use read flooding or write flooding.
  • Accurate calculation of message unread count.
  • Group receipt message.
  • Live more in different places.

If you are interested in these issues, you can also refer to the excellent IM learning website (instant messaging network: http://www.52im.net/ ).

In the process of self-development, some functions can also directly use mature products on the market, such as file storage, security audit, and offline message push . To speed up the pace of research and development. If you decide to develop this part of the function yourself in the future, you can also easily replace it to achieve twice the result with half the effort.

For enterprises with economic and development strength, and the business is expected to have a large number of customers, it is recommended to adopt the self-developed implementation method, which is in line with the planning of later capacity expansion, rapid iteration, and stable operation and maintenance.

However, self-research needs to invest a lot of manpower and financial resources. It is recommended that developers who want to do self-research should make a clear development plan to reduce unnecessary losses.

The introduction of open source projects cannot expand new functions very well, and the operation and maintenance are complicated, making it difficult to support long-term development in the future; the self-developed route has a long cycle and high cost.

Is there a compromise solution? It can be launched quickly without investing such a large cost, and can also be customized. Integrating the commercial SDK is the most convenient way, which is also the mainstream development mode at present. Now SDK manufacturers are very mature, and many companies will choose this method.

The third implementation method: Integrate commercial IM SDK to realize IM instant messaging

Integrated commercial SDK has the following advantages:

  • Quickly land IM products, go online quickly, and seize the market.
  • Stable service, avoid complicated operation and maintenance work.
  • Functions can be extended: When adding new functions, you can actively ask the SDK manufacturer for implementation.
  • Compared with self-research, the cost is greatly reduced. ##

IM Instant Messaging Product Landing Process

When selecting an integrated commercial SDK, the product landing process is as follows:

  • Apply for an sdk vendor service account and obtain an account key for user login.
  • Develop a business service background to calculate login authentication information.
  • Integrate the manufacturer's sdk and develop applications (such as iOS applications, Android applications, applets, etc.). Taking sending and receiving messages as an example, the interaction process between modules is as follows:

insert image description here

It can be seen that the solution of integrating commercial SDK only needs to develop a simple business background, and then integrate the SDK to develop its own application, and then the service can be launched quickly.

Recommended IM SDK Vendors - Instant IM SDK

There are already many mature IM SDK manufacturers on the market, and here is a good manufacturer-Jiegou Technology ( https://doc-zh.zego.im/article/11598 ). The live broadcast product developed before is connected to the RTC SDK, and the whole access process is very smooth. Recently, because the project needs to realize the instant messaging function, the same manufacturer plans to connect to the ZEGO IM SDK with a tentative attitude. I did not expect to complete the development soon Achieved.

ZIM supports all mainstream platforms, including two cross-platform frameworks, flutter and uniapp, to accelerate product launch. In terms of message security auditing, they use the services of mainstream third-party security vendors, which can basically support the required auditing functions.

With the continuous development of business, real-time communication projects have higher requirements for high availability/high concurrency/low latency of communication services. Before using their RTC products, the low-latency industry-leading performance is very good. I tested the IM product with the integrated demo, and the end-to-end delay is only tens of milliseconds.

Instant IM products not only support basic single chat/group chat functions, but also support room chat with high message concurrency. The official website data shows that the number of people in a single room can support more than one million, which is suitable for scenarios with high requirements for the number of people in the room. In addition, there is a very novel call invitation function to meet the needs of instant messaging.

Experience Sharing: Realizing Instant Messaging Function Based on Instant ZIM

According to the project requirements, the author chooses ZIM to realize the single-chat scene message sending and receiving, and the whole process can be completed in half a day with only 2 simple steps. The following uses my own experience to share how to quickly realize message sending and receiving in single-chat scenarios. Friends who are interested in integrating the sdk process can go to the official website of Zego ( https://doc-zh.zego.im/article/11598 ), so I won’t go into details here.

Peer-to-peer messages are more common in IM usage scenarios. Here we take text messages sent and received on the Android side as an example.

3 easy steps to send and receive instant messaging messages

1. Initialize IM SDK

Get a ZIM instance

zim = ZIM.create(appID, application);

2. Login to ZIM

Similar to the operation of WeChat account login, it is used as a carrier for sending and receiving messages

void login(ZIMUserInfo userInfo,String token,ZIMLoggedInCallback callback)

Corresponding UI example:

insert image description here

3. The sender calls to send a single chat text message

After logging in, you can call this interface, fill in the message you want to send in message, fill in the userID of the receiving end in toUserID, and call it when you want to send

Interface display:

void sendPeerMessage(ZIMMessage message,String toUserID,ZIMMessageSendConfigconfig,ZIMMessageSentCallback callback)

Corresponding UI example:

insert image description here

4. The IM receiver receives messages

(1) Register the receiving object of the event callback through setEventHandler

Various events will occur during the running of IM: a message is received, the network connection is interrupted, etc. Through this interface, the events thrown by ZIM can be received so that the App can respond accordingly.

void setEventHandler(ZIMEventHandler handler)

(2) Rewrite the method of receiving single-chat messages in the receiving object of the registration event callback

zim.setEventHandler(new ZIMEventHandler() {
    @Override
    public void onReceivePeerMessage(ZIM zim, ArrayList<ZIMMessage> messageList, String fromUserID) {
   
   }
});

Corresponding UI example

insert image description here

Thus we realize the sending and receiving of point-to-point text messages.

By the way, ZIM SDK also supports the sending and receiving of rich media messages, including pictures, videos, audios and files. When sending a rich media message, you only need to pass the path of the file into the interface, and the upload progress can be obtained from the progress callback.

5. Diversified message sending and receiving: sending rich media messages

void sendMediaMessage(ZIMMediaMessage message,String toConversationID,ZIMConversationType conversationType,ZIMMessageSendConfigconfig,ZIMMessageSentCallback callback)

UI example, take sending a picture as an example, read the picture from the photo album and compress it and save it in the APP directory, pass the path of the local picture to ZIMImageMessage, and call sendMediaMessage, and display it on the UI accordingly.

insert image description here

Once peer-to-peer messages and group chat messages are sent for the first time, the corresponding conversations are created.

insert image description here

We know that UI updates are all driven by data. What drives the UI changes here is the session update event callback thrown by ZIMEventHandler. The application layer only needs to maintain a session list, update the list in time when this event is thrown, and drive the UI refresh.

session update interface

void onConversationChanged(ZIM zim,ArrayList<ZIMConversationChangeInfo> conversationChangeInfoList)

Conclusion: Instant IM SDK combined with RTC SDK to realize audio/video/live real-time chat

In addition, the ZIM SDK also supports the use of rooms and groups, without the need to repackage, draw the relevant UI and use the data driver provided by the SDK interface to realize the corresponding functions. No more descriptions here, interested authors will update related articles later, or go to ZEGO official website to view related documents: https://doc-zh.zego.im/article/14314?

At the same time, ZIM SDK combines with ZIM's own RTC SDK to realize user interaction in various audio and video scenarios, which is suitable for developers and enterprises in need of scenarios such as Avatar, live broadcast, and chat rooms.

Developers who have development plans in the near future can check it on the official website of iGoo, which coincides with the 7th anniversary of iGou with a 10% discount on all audio and video/live broadcast products, suitable for small and medium-sized enterprises and personal development studios with budget requirements.

The author strives for a small benefit for everyone: after submitting the form, professional sales will contact you, and you can get a 1-month free trial of Instant IM SDK.

Fill in and collect: < https://www.zego.im/cluesCollect >

Guess you like

Origin blog.csdn.net/RTC_SDK_220704/article/details/125876799