This article is by the original author “crossoverJie (Chen Jie)” original share, instant messenger network included changes and revisions, thanks to the original author.


1, the introduction



Technical dry goods: from scratch, teach you to design a million – level notification push system






CIM






By studying this article and the CIM code, you can gain the following knowledge:





  • 1) How to develop a set of IM from scratch (CIM’s customers are a little weak, forgive me);
  • 2) How to design distributed IM architecture;
  • 3) How to implement your distributed IM architecture with code and related technologies.



CIM source code address:





  • Main image: github.com/crossoverJi…
  • Standby image: github.com/52im/cim



The following articles are similar or related to this article and will also help you get started with IM development:





  • From the development of IM is so difficult? Hand to hand teach you from a Simple Andriod version OF IM (source)
  • For beginners: Developing an IM Server from Scratch (Netty based, complete source code available)
  • Pick up the Keyboard and do it: Develop a Distributed IM System with my Hands
  • Brief Discussion on IM System Architecture Design
  • The Pits of Mobile IM Development: Architectural Design, Communication Protocols, and Clients
  • Sharing of mobile TERMINAL IM Architecture Design Practice of a Set of Massive Online Users (with detailed pictures and texts)
  • Theoretical Architecture Scheme of an Original Distributed Instant Messaging (IM) System
  • Design practice of a set of highly available, Scalable and Concurrent IM Group chat and Single Chat Architecture



* Friendly tips:
Just one entry for beginners: Developing mobile IM from scratch


2. About the author







CrossoverJie (Chen Jie) :



The author’s blog:
crossoverjie.top



The author’s Github:
Github.com/crossoverJi…


3. Run the demo









CIM Private Chat video demonstration:
www.bilibili.com/video/av394…



CIM group chat video demonstration:
www.bilibili.com/video/av394…


4. Architectural design



Let’s look at the specific architecture design:










Architecture description:





  • 1) All components of CIM are constructed by SpringBoot;
  • 2) Use Netty + Google Protocol Buffer to build the underlying communication;
  • 3) Redis stores routing information, account information and online status of each client;
  • 4) Zookeeper is used to register and discover im-server services.



The whole is mainly composed of the following modules:





  • 1) CIM -server — IM server: used to receive client connection, message transparent transmission, message push and other functions. Supports cluster deployment.
  • 2) CIM -forward-route — Message routing server: used to process message routing, message forwarding, user login, user logout and some operation tools (obtaining online users, etc.);
  • 3) CIM -client — IM client: a messaging terminal for users, which can be started with a command and initiate communication with others (group chat or private chat); At the same time built-in some common commands for easy use.


5. Logical flow chart



The overall process is also relatively simple, the flow chart is as follows:










The process is explained as follows:





  • 1) The client initiates login to route;
  • 2) If the login succeeds, select an available IM-server from Zookeeper and return it to the client, and save the login and routing information to Redis.
  • 3) The client initiates a long connection to the IM-server and maintains the heartbeat after the connection succeeds.
  • 4) Clear the client status through route when the client goes offline.



So when we deploy ourselves we need the following steps:





  • 1) Build basic middleware Redis and Zookeeper;
  • 2) Deploy the CIM-Server, which is a real IM server that supports horizontal scaling to meet performance requirements. You only need to register with the same Zookeeper.
  • 3) Deploy the CIM-forward-route, which is the routing server through which all messages pass. Since it is stateless, you can also use Nginx agents to improve availability;
  • 4) CIM -client is a real user-oriented client; After startup, it will automatically connect to the IM server and you can send and receive messages on the console.



Quick start








6. IM server



Let’s start with the server side:






The first is service startup:




















Protobuf
Protobuf communication protocol detail: code demonstration, detailed principle introduction, etc


7. Register discovery



Handle service registration and discovery






Therefore, you need to register your own data with Zookeeper after the application is successfully started.














The main purpose is to register the current application IP + cim-server-port+ http-port:












8, login









As shown in the previous flow:





























9. Offline messages


















10. IM routes















10.1 Registering an Interface

























10.2 Login Interface



The login in this section is different from the login in CIM – Server.










Specific process:





  • 1) After successful login, check whether the login is repeated (one user can run only one client).
  • 2) After the login is successful, you need to obtain the service list (CIM – Server) from Zookeeper and select a service based on an algorithm to return it to the client.
  • 3) After the successful login, the routing information needs to be saved, that is, the service instance assigned by the current user is saved in Redis.
















Similar to Java’s HashSet, it can only be resaved.



Obtaining an available routing instance is also simple:






  • 1) Obtain all service instances from Zookeeper to make an internal cache;
  • 2) Polling to select a server (currently only this algorithm, will be added later).









The specific code is as follows:


















Guava





10.3 Group Chat Interface
































Routing relationships are stored in Redis as follows:






















The implementation of cim- Server is as follows:



















10.4 Online User Interface

























10.5 Private Chat Interface









Something like this:










In our scenario, the prerequisite for private chat is to obtain the userID of the online user:















10.6 Offline Interface



















11. IM client








11.1 the login























































11.2 User-defined Protocols



Google Protocol Buffer













In fact, there are only three fields in this protocol:





  • 1) requestId can be understood as userId;
  • 2) reqMsg is the real message;
  • 3) Type of message



At present, there are three types, corresponding to different services:









11.3 the heart









The current policy is to send a heartbeat packet to the server every minute:














The server will receive a ping heartbeat packet every minute if it does not receive a service message:









11.4 Built-in Commands






























































11.5 group chat















11.6 the private chat
















11.7 Message Callback




























11.8 Customizing the Interface











12. Summary of this paper









Follow-up plan:






Appendix: More IM related articles



[1] Articles on IM code practices:



Is it so hard to develop IM yourself? Hand-in-hand teach you from a simple Andriod version OF IM (source code)



Discussion on the design and Implementation of an INTELLIGENT IM Heartbeat Algorithm on Android terminal



Teach you to use Netty to realize the heartbeat mechanism of network communication procedures, disconnection reconnection mechanism



Netty security in Detail: Introduction to the principles, code demo (Part 1)



Netty security in Detail: Introduction to the principles, code demo (Part 2)



Cracked version of wechat local database (including iOS and Android), only for study and research



MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA MINA



Lightweight im framework MobileIMSDK iOS source (open source version)



Open source IM project “Mogujie TeamTalk” before May 2015 Uncut version complete code



Cracked version of wechat local database (including iOS and Android), only for study and research



NIO framework introduction (a) : Server based on Netty4 UDP two-way communication Demo Demo



NIO framework introduction (ii) : Server based on MINA2 UDP bidirectional communication Demo Demo



NIO framework introduction (iii) : iOS and MINA2, Netty4 cross-platform UDP bidirectional communication practice



NIO framework introduction (4) : Android and MINA2, Netty4 cross-platform UDP two-way communication actual combat



For IM image compression Android tool class source, the effect can be comparable to wechat



High imitation Android version of mobile QQ can drag not reading small bubble source



Node.js +socket.io



Android chat interface source code: to achieve the chat bubble, expression icon (can turn the page)



High imitation Android version of mobile QQ home page side slider menu source



Open source Libco library: the foundation of background framework supporting 800 million users of wechat



Share Java AMR audio file merge source code, the most complete network



Android source obfuscation tool: AndResGuard



A complete Android push Demo based on MQTT communication protocol



Android version of high imitation wechat chat interface source code



High imitation mobile QQ Android version lock screen chat message reminder function



High imitation iOS version of mobile QQ recording and amplitude animation complete implementation



Comment and Reply functions in Android social applications



Implementation of @person function in Android IM application: Imitation of Weibo, QQ, wechat, zero intrusion, high extensibility



Imitated wechat IM chat time display format (including iOS/Android/Web implementation)



Android version copy wechat circle of friends picture drag return effect



For beginners: Developing an IM server from scratch (based on Netty, with complete source code)



Pull up the keyboard and do it: masturbate a distributed IM system with me



More of the same…






[2] Articles on IM Architecture design:



Brief talk about IM system architecture design



Briefly describe the pitfalls of mobile IM development: architectural design, communication protocols, and clients



Sharing of a set of mobile TERMINAL IM Architecture Design practice of massive Online Users (with detailed pictures and texts)



A set of original distributed instant messaging (IM) system theoretical architecture scheme



From zero to excellence: The evolution of the technical architecture of JINGdong customer service INSTANT messaging system



Architecture selection for mogujie INSTANT messaging /IM server development



Tencent QQ140 million online users technical challenges and architecture evolution road PPT



Design practice of time-sequence-based cold and hot classification architecture for massive data in wechat background



Speech by technical Director of wechat on Architecture: The Way of wechat — Avenue to Simplicity



How to interpret “wechat Technical Director on Architecture: The Way of wechat — The Road to Simplicity”



Fast Fission: Witness the evolution of wechat’s powerful background architecture from 0 to 1 (I)



17 years of practice: Technical methodology of Tencent’s massive products



How to ensure the efficiency and real time of pushing large-scale group messages in MOBILE IM?



Discussion on synchronization and storage scheme of chat messages in modern IM system



How to design a large number of image file server storage architecture?



Quick understanding of server database read/write separation principles and practical suggestions



Understand cookies, sessions, and tokens in HTTP short connections



WhatsApp technology Practice Sharing: Technology myth created by 32 engineering team



Technical challenges and practice summary behind 100 billion visits of wechat circle of Friends



Behind king of Glory’s 200 million users: product positioning, technical architecture, network scheme, etc



Selection of MQ messaging middleware for IM systems: Kafka or RabbitMQ?



Tencent senior architect dry goods summary: one article to understand the design of large distributed system aspects



Taking microblog application scenarios as an example, this paper summarizes the architectural design steps of massive social system



Quickly understand the principles of high-performance HTTP server load balancing



Bullet SMS behind the bright: netease yunxin chief architect to share 100 million IM platform technology practice



Zhihu technology sharing: From single machine to 20 million QPS concurrent Redis high performance cache practice road



IM development basics tutorial (five) : easy to understand, correctly understand and use MQ message queues



Wechat Technology Sharing: Practice of generating serial number of wechat’s Massive IM Chat Messages (Algorithm Principle)



Wechat Technology Sharing: Practice of generating serial numbers of wechat’s massive IM Chat Messages (Disaster Recovery Solution)



Beginner: Zero-based understanding of evolution history, technical principles, and best practices of large-scale distributed architecture



A set of high availability, easy to scale, high concurrency IM group chat, single chat architecture design practice



Ali Technology Sharing: In-depth insight into the 10-year history of Ali database technology solutions



Ali technology sharing: The arduous growth path of OceanBase, a financial database developed by Ali



Social software red envelope technology decryption (a) : comprehensive decryption QQ red envelope technology scheme – architecture, technical implementation, etc



Social software red envelope technology decryption (II) : decrypt wechat shake a red envelope from 0 to 1 technology evolution



Social software red envelope technology decryption (three) : wechat shake a red envelope rain behind the technical details



Decryption of social software Red envelope technology (IV) : How does wechat red envelope system cope with high concurrency



Decryption of social software Red envelope technology (5) : How does wechat red envelope system achieve high availability



Decryption of social software Red envelope technology (VI) : Evolution practice of storage layer architecture of wechat red envelope system



Social software red envelope technology decryption (seven) : Alipay red envelope mass high concurrency technology practice



Social software red envelope technology decryption (eight) : full decryption micro blog red envelope technology scheme



(9) : Talk about the functional logic, disaster recovery, operation and maintenance, architecture, etc



Instant Messaging beginner: What is Nginx? Can it implement IM load balancing?



Instant Messaging Novice: A quick understanding of RPC technology – basic concepts, principles, and uses



With a multi-dimensional comparison of the five major distributed MQ message queues, mom no longer had to worry about my technology selection



From guerrilla to regular Army: the evolution of IM System architecture of Hornet’s Nest Tourism Network



IM development basic knowledge make-up lesson (six) : database with NoSQL or SQL? Just read this!



More of the same…