当前位置:网站首页>Im instant messaging development: Practice of offline messages and historical messages
Im instant messaging development: Practice of offline messages and historical messages
2022-06-09 22:38:00 【wecloud1314】
In today's mobile Internet era ,IM Such products have become an indispensable part of our life . Like wechat 、 nailing 、QQ And so on are typical IM Social products with core functions . There are also some applications, although IM Function is not the core , but IM Capability is also an extremely important part of its whole application , Like online games 、 E-commerce live broadcast and other applications .
stay IM Under the premise of more and more extensive technology application scenarios , For instant messaging IM It is more and more necessary to learn and master technology .
stay IM In a huge technical system , The message system is undoubtedly the core , And in the message system , The most critical part is the distribution and storage of messages , Offline messages and historical messages are unavoidable technical points in this key link .

IM The general practice of message delivery
In general IM In the message system , For real-time messages 、 offline message 、 The historical news is probably the following technical ideas .
For online users : Messages are sent directly to online recipients in real time , When the message is sent , The server does not store messages on the ground .
For offline users : The server will store the message to the offline library , When the user logs in , Pull offline messages away from the offline Library , Then the server will delete the offline message .
The disadvantage of this implementation is that the message is not persistent , The message roaming cannot be supported , Reduces the reliability of messages .
(PS: actually , In fact, this is not a disadvantage , Because it is not necessary to store historical messages in some scenarios , The so-called message roaming capability is not necessary , For example, wechat .)
And in the message system we designed , As long as the server receives the message from the sender , While forwarding to the receiver, the message will also be stored on the ground in the offline database and historical message library , The landing of historical messages can also support message roaming and other related functions .
What are offline messages and historical messages ?
About offline messages and historical messages , On the technical , We define it this way .
1) offline message :
Offline messages are users ( The receiver ) Messages received while offline , Most of these messages are concerned by users , It has certain timeliness .
In terms of our system experience , By default, our offline messages only save the messages of the last seven days .
user ( The receiver ) After the next login, you will get all these offline messages , Then, the client performs offline message processing according to the chat session UI Exhibition ( For example, display an unread message bubble ).
(PS: The possibility of users offline is technically composed of many situations , For example, the other party is not online 、 The other party's network is broken 、 The other party's mobile phone crashed 、 The server made an error while sending, etc , Strictly speaking —— As long as the message cannot be sent in real time , All count “ offline message ”.)
Historical news :
Historical messages store all chat messages of users , These messages include outgoing messages and received messages .
When the client gets the history message , It is usually obtained by paging according to the session .
In terms of our system experience , The storage time of historical messages is designed to be half a year by default , Of course, this time can be determined according to the actual product operation rules , There are no hard and fast rules .

When the user sends a chat message to the server , First it goes into the messaging system , The message system distributes and stores messages .
In the process : For online recipients , Will choose to push the message directly . But when the receiver is not online or the message push fails , There will also be other ways to get messages , For example, the receiver will take the initiative to pull the unreceived messages from the server . But it is unknown when and where the receiver will pull the message from the server , So that's the point of storing messages in the offline library .
The message system stores the process offline , In order not to affect the stability of the whole system , We used MQ Message queuing IO Uncouple , So chat messages are actually stored asynchronously in the offline library ( adopt MQ Slow progress IO Uncouple , This is also the usual practice ).
After distributing the message : The message service will synchronize a message data to the historical message service , The historical message service will also store messages on the ground .
For new client devices : There will be a need to synchronize messages ( The so-called message roaming capability ), And this is the main function of historical news . In the historical message base , The client can pull the full amount of historical messages of any session .
1) Our storage medium for offline messages is Redis;
2) Historical news we chose HBase.
Why choose different storage media , In fact, we are considering different business scenarios and read-write modes of offline messages and historical messages .
Let's focus on the difference between offline message storage and historical message storage .
Offline message storage mode ——“ I want to write ”
For the storage mode of offline messages, we use diffusion writing .
Each user has their own separate in box and outbox :
1) The inbox stores all messages that need to be synchronized to the receiving end ;
2) All messages sent by the sender are stored in the Outbox .
Take single chat as an example : In a conversation between two people , The message will be written twice , That is, the sender's outbox and the receiver's inbox .
And in the group scenario : Writing will be magnified more ( Spread ), If there are N personal , That group message will be spread and written N Time .
To sum up :
1) The advantage of diffusion writing is : The logic of the receiver will be very clear and simple , Just read it once from your in box , This greatly reduces the reading pressure required to synchronize messages ;
2) The disadvantage of diffusion writing is : The write is magnified exponentially , Especially for the group scenario .
Historical message storage mode ——“ Spread reading ”
The storage mode of historical messages is diffusion reading . Instant messaging chat software app Development can consult weikeyun .
Because in historical news , Each session holds the full amount of messages for the entire session . In diffuse reading mode , Messages are saved only once per session .
Contrast diffusion write mode , The advantages and disadvantages of diffusion reading are as follows :
1) Advantage is : The number of writes is greatly reduced , Especially for group messages , Just save it once ;
2) The disadvantage is that : Receiving messages at the receiving end is very complex and inefficient , Because in this mode, if the client wants to pull all messages, it can only synchronize once per session , Reading will be magnified , And there may be many invalid reads , Because some conversations may have no news at all .
边栏推荐
- M-arch (fanwai 14) gd32l233 evaluation - driving segment code LCD
- 2022 safety production month activity starts safety production and epidemic prevention and control
- 数据库优化方面的经验
- Maximum value and subscript of acquisition matrix of C language test question 168
- 浅谈倍增法求解LCA
- 这本书押中了2022北京高考作文题
- C语言试题162之圆周率π
- 在 4GB 物理内存的机器上,申请 8G 内存会怎么样?
- 【图像重建】基于正则化的图像超分辨重建附matlab代码
- mkdir 创建目录命令
猜你喜欢

Web3在遥远的未来?不,它已经来了!

Digital integrated management system of double prevention system in chemical enterprises

This book has won the 2022 Beijing college entrance examination composition

华为设备配置Hub and Spoke

15省份发布2021年平均工资,这些行业有“钱途”

关于mongodb的那些安装、配置、报错处理、CRUD操作等再总结

C语言试题164之求定积分
![[launch] modify the app theme according to the wallpaper. It really comes](/img/a9/c808005f4f2f55ae2df94053f5892c.png)
[launch] modify the app theme according to the wallpaper. It really comes

房贷利率下调 现在是买房的时机吗?

The survey shows that MacOS application developers generally say that their biggest challenge is how the product is discovered by users
随机推荐
What is the "big safety" industry? Digital empowerment and great safety industry development
Veracrypt create file type encrypted volume
Do your filial duty to make an old people's fall prevention alarm system for your family
Find my product | the new TWS headset supports the find my function, and apple find my is more widely used in third-party manufacturers
这本书押中了2022北京高考作文题
[Luogu p8330] mode (radical divide and conquer)
Matrix transpose of C language test question 170
【刷题篇】布尔运算
华为设备配置Hub and Spoke
ffmpeg加opencv的人脸采集并做出识别的实战项目!
YUV格式与RGB格式
At present, I am 28 years old, and I have successfully won the 15K salary since I started to wander to the north in 20 years and started to test the entry software
[filter] speech denoising based on time-varying Wiener filter, including Matlab source code
[BP prediction] BP neural network based on AdaBoost realizes data regression prediction with matlab code
The original tree array can be so simple?
【刷题篇】最长递增子序列
The Little Schemer 中文版
[bearing fault decomposition] realize bearing fault signal decomposition based on ITD, including Matlab source code
C语言试题168之获取矩阵的最大值及其下标
2022年最系统的自动化测试,测试开发面试题,10k以下不建议看