This article is shared by the author "Chinese cabbage", and there are changes. Note: This series is an article for IM beginners, IM old fritters still look to Haihan, don't spray!
1 Introduction
This is another IM coding practice article based on Netty. Because the content of one article is too long to read, it is too tiring to read, so it is divided into 4 articles according to the author's idea, and the psychological pressure of reading is not so great.
Several articles in this series share: Assuming that there is no established third-party IM library or SDK, with the basic technical vision of network programming, think and practice how to write a chat based on the Netty network library from scratch. In the process of IM system, there is no dazzling architecture design, and no high-end and atmospheric pattern design methodology. Some are just ideas and actual combat from the perspective of IM beginners, which are suitable for IM beginners to read.
This article is mainly the beginning of the IM series with bare hands. It mainly explains the design ideas of IM, and does not involve practical coding. I hope it will help you.
study Exchange:
- Introductory article on mobile IM development: "One entry is enough for beginners: developing mobile IM from scratch"
- Open source IM framework source code: https://github.com/JackJiang2011/MobileIMSDK (click here for alternate address)
(This article has been published simultaneously at: http://www.52im.net/thread-3963-1-1.html )
2. Knowledge preparation
- Important note: This series of articles is mainly about code practice sharing. If you don’t know much about the technical theory of instant messaging (IM), it is recommended to read in detail: "Introduction to Zero-Basic IM Development: What is an IM System? ", "One entry for beginners is enough: developing mobile IM from scratch".
Don't know what Netty is? Here is a brief introduction:
Netty is an open source framework for Java. Netty provides an asynchronous, event-driven network application framework and tools for rapid development of high-performance, high-reliability network server and client programs.
In other words, Netty is a NIO-based client and server-side programming framework. Using Netty can ensure that you can quickly and easily develop a network application, such as a client and server-side application that implements a certain protocol.
Netty considerably simplifies and streamlines the programming and development process of network applications, such as TCP and UDP socket service development.
A good article on the basics of Netty:
1) Getting started: The most thorough analysis of Netty's high-performance principles and framework architecture so far
2) For beginners: learning methods and advanced strategies of Java high-performance NIO framework Netty
3) The most popular Netty framework entry long article in history: basic introduction, environment construction, hands-on combat
If you don't even know what NIO in Java is, the following articles are recommended to be read first:
1) Less verbose! Take you to understand the difference between Java's NIO and classic IO in one minute
2) Introduction to the strongest Java NIO in history: If you are worried about getting started and giving up, please read this!
3) Java's BIO and NIO are difficult to understand? Use code practice to show you, if you don't understand me again, I will change careers!
Online access to Netty source code and API:
1) Netty-4.1.x complete source code (online reading version) (*recommended)
2) Netty-4.1.x API documentation (online version)
3. Series of articles
This article is the first in a series of articles, the following is the series table of contents:
"Based on Netty, Freehand IM (1): IM System Design" (* this article)
"Based on Netty, Freehand IM (2): Coding Practice (Single Chat Function)"
"Based on Netty, Freehand IM (3): Coding Practice (Group Chat Function)"
"Based on Netty, Freehand IM (1): Coding Practice (System Optimization)"
4. Demand analysis
Business scenario: This actual combat is to simulate WeChat's IM chat, each client establishes a connection with the server, and can realize point-to-point communication (single chat) and point-to-multipoint communication (group chat).
Design idea: What we want to achieve is point (client) to point (client) communication, but in most cases, the business we contact is the communication between the client and the server (the so-called C/S mode? ), the client only needs to know the IP address and port number of the server to initiate communication. So how should the client and the client be designed?
Technical thinking: Is it a communication connection (so-called P2P) between mobile phones and mobile phones to send messages to each other?
This solution is obviously not a good solution:
1) First of all: For the communication between the client and the client, you first need to determine the IP address and port number of the other party, which is obviously not very realistic;
2) Second: Even if there is a way to get the IP address and port number of the other party, each point (client) has to act as both a server and a client, which invisibly increases the pressure on the client.
In fact, we can use the server as a transfer station for IM chat messages, and the server can actively push messages to the designated client. If it is this mode, then the Http protocol cannot be supported (because Http is stateless and can only be a request-response mode), so it can only be implemented using the TCP protocol.
Jack Jiang's Note: The author's statement here is not very accurate, because although HTTP is stateless, it can also achieve instant messaging capabilities. Interested readers can read the following articles to learn about these people who have used HTTP to achieve instant messaging and chat. Technical method:
"Beginner's Guide: The Most Complete Web-side Instant Messaging Technology Principle Detailed Explanation"
"Inventory of Web-side Instant Messaging Technologies: Short Polling, Comet, Websocket, SSE"
"Quick Start of Web-side IM Communication Technology: Short Polling, Long Polling, SSE, WebSocket"
5. IM single chat idea design
5.1 Principle of Communication Architecture The following is a schematic diagram of the communication architecture:
As shown in the figure above, the communication process is analyzed as follows:
1) To realize the communication between the client and the client, the server needs to be used as a transfer station for communication, and each client must establish a connection with the server;
2) After each client establishes a connection with the server, the server saves the mapping relationship between the user ID and the channel, where the user ID is the unique identifier of the client;
3) When client A sends a message to client B, it first sends the message to the server, and then the server pushes it to client B.
For the above point "3)", how does the server find client B?
When client A sends a message to the server, the information carried in the message includes: "user ID of client A", "user ID of client B", and "message content". In this way, the server can successfully find the channel of server B and push the message.
5.2 Message Push Process When each client establishes a connection with the server, the personal user information must be uploaded to the server, and the server keeps the mapping relationship uniformly. If a client goes offline, the server listens to the disconnection and deletes the corresponding mapping relationship.
Second: when initiating a group chat, the touser field needs to be passed, and the server finds the corresponding connection channel in the mapping table according to this field and initiates a message push.
The above logic principle is shown in the following figure:
5.3 More details In fact, there are still a lot of technical details to consider before actually doing IM. The following articles have reached the typical hot technical points of IM. Those who are interested must read it:
"Technical Problems That Mobile IM Development Needs to Face"
"Talking about the optimization of login requests in mobile IM development"
"Implementation of IM Message Delivery Guarantee Mechanism (1): Ensuring Reliable Delivery of Online Real-time Messages"
"Implementation of IM Message Delivery Guarantee Mechanism (2): Ensuring Reliable Delivery of Offline Messages"
"How to ensure the "sequentiality" and "consistency" of IM real-time messages? 》
6. IM group chat idea design
Group chat refers to the chat between multiple users in a group, and a message sent by a user to the group will be received by any member of the group.
The specific architectural ideas are as follows:
As shown in the figure above, the group chat communication process is analyzed as follows.
1) The group chat is actually the same as the single chat as a whole. It is necessary to save the corresponding relationship between each user and channel, so that it is convenient to find the corresponding channel through the user ID later, and then follow up the channel to push messages.
2) How to send messages to members of multiple groups?
In fact, it is very simple. The server saves another mapping relationship, that is, the mapping relationship between chat rooms and members. When sending a message, first find all the corresponding members according to the chat room ID, then follow up the ID of each member to find the corresponding channel, and finally send the message by each channel.
3) When a member joins a group chat group, a new record is added to the mapping table, and if a member leaves the group, the corresponding mapping record is deleted.
From the above architecture diagram, it can be found that compared with single chat, group chat is actually just one more mapping relationship.
In fact, group chat is a relatively technically difficult function in IM. Interested readers can read the following articles:
"Should "Push" or "Pull" be used for online status synchronization in IM single chat and group chat? 》
"IM group chat messages are so complicated, how to ensure that they are not lost or heavy? 》
"How to ensure the efficiency and real-time performance of large-scale group message push in mobile IM? 》
"Discussion on Synchronization and Storage Scheme of Chat Messages in Modern IM System"
"Discussion on the disorder of IM instant messaging group chat messages"
"How to implement the read receipt function of IM group chat messages? 》
"Is the IM group chat message stored in one copy (ie, diffusion reading) or multiple copies (ie, diffusion writing)? 》
"A set of high-availability, easy-to-scale, high-concurrency IM group chat, single chat architecture design practice"
In addition, for ultra-large-scale group chats, the technical difficulty increases exponentially:
"NetEase Yunxin Technology Sharing: Summary of the Practice of Ten Thousand Crowd Chat Technology Solutions in IM"
"Alibaba Dingding Technology Sharing: The King of Enterprise IM - Dingding's Superiority in Back-end Architecture"
"Discussion on the realization of the read and unread function of IM group chat messages in terms of storage space"
"Demystifying the IM Architecture Design of Enterprise WeChat: Message Model, Ten Thousand Crowds, Read Receipts, Message Withdrawal, etc."
"Rongyun IM Technology Sharing: Thinking and Practice of the Message Delivery Scheme for Thousands of People Chat"
"WeChat Live Chat Room Single Room 15 Million Online Message Architecture Evolution Road"
7. Summary of this article
This article is mainly to help readers master the core design ideas of single chat and group chat.
Single chat: Mainly, the server saves a mapping relationship between users and channels. When sending a message, it finds its corresponding channel channel according to the recipient ID, and the channel's write() can send messages to the client.
Group chat: Save two relationships, namely the relationship between user ID and Channel, and the relationship between group ID and user ID. When pushing a message, first find the corresponding member according to the chat group ID, traverse each member and then find out the corresponding channel.
Overall, the idea is still very simple. After mastering the design idea, you will find that designing an IM chat software is actually not very complicated.
8. Related articles
If you feel that this series of articles is not detailed enough, you can systematically study the following series of articles:
"Learn IM from the source code (1): teach you to use Netty to implement the heartbeat mechanism, disconnection and reconnection mechanism"
"Learn IM with source code (2): Is it difficult to develop IM by yourself? Teach you to play an Android version of IM"
"Learn IM with the source code (3): Based on Netty, develop an IM server from scratch"
"Learn IM with the source code (4): Pick up the keyboard and do it, teach you to develop a distributed IM system with your bare hands"
"Learn IM from the source code (5): correctly understand the IM long connection, heartbeat and reconnection mechanism, and implement it by hand"
"Learn IM from the source code (6): teach you how to quickly build a high-performance and scalable IM system with Go"
"Learn IM from the source code (7): teach you how to use WebSocket to create web-side IM chat"
"Learn IM from the source code (8): 10,000-character long text, teach you how to use Netty to create IM chat"
"Learn IM with Source Code (9): Implementing a Distributed IM System Based on Netty"
"Learn IM with the source code (10): Based on Netty, build a high-performance IM cluster (including technical ideas + source code)"
"SpringBoot integrates the open source IM framework MobileIMSDK to realize instant messaging IM chat function"
9. References
[1] Getting Started for Novices: The most thorough analysis of Netty's high-performance principles and framework architecture so far
[2] Combining theory with practice: a detailed explanation of a typical IM communication protocol design
[3] Talking about the architecture design of IM system
[4] Briefly describe the pits of mobile IM development: architecture design, communication protocol and client
[5] A set of practice sharing of mobile IM architecture design for massive online users (including detailed pictures and texts)
[6] A set of original distributed instant messaging (IM) system theoretical architecture scheme
[7] A set of high-availability, easy-to-scale, high-concurrency IM group chat, single chat architecture design practice
[8] A set of IM architecture technology dry goods for hundreds of millions of users (Part 1): overall architecture, service splitting, etc.
[9] A set of IM architecture technology dry goods for hundreds of millions of users (Part II): reliability, orderliness, weak network optimization, etc.
[10] From novice to expert: how to design a distributed IM system with hundreds of millions of messages
[11] Based on practice: Summary of technical points of a small-scale IM system with millions of messages
[12] Tantan's IM long connection technology practice: technology selection, architecture design, performance optimization
(This article has been published simultaneously at: http://www.52im.net/thread-3963-1-1.html )
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。