Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the SESSION structure of IM system?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the SESSION structure of the IM system. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. The basic structure of IM system

The basic structure of an IM system is shown in the following figure

1. The msg-gate module is the access layer, and the core function is connection rectification. Maintain a massive TCP persistent connection with the client, and rectify the external massive TCP persistent connection to a small amount of TCP persistent connection to the back-end msg-logic.

2. Msg-logic module is the logic layer, and the core function is to deal with message delivery logic.

3. Redis is responsible for caching the connection status of the business client. Which msg-gate is connected to, and whether the connection status is normal. Used to provide message routing when pushing a message to a user.

4. Ipconfig provides the client with the IP address where msg-gate is located.

II. Session maintenance methods and reasons

1. Session maintenance module

There are two modules to maintain session in the whole system, msg-gate and redis.

2. Reasons for maintaining session in two places

Msg-gate is a distributed deployment, with each node connected to a different client (client). When msg-gate receives client messages or logic requests to send messages to client, you need to verify whether the client status is normal. From a performance point of view, msg-gate needs to maintain a session locally.

Because msg-gate,msg-logic is a distributed deployment, there are several nodes. When clientA sends messages to clientB, msg-logic needs a place to retrieve the msg-gate location (IP) to which clientB is connected, so it needs a centralized (or hash multiple machines) location to store session routing relationships.

Third, the specific structure of session storage

1. Msg-gate module session structure

The session structure of msg-gate is given first.

The session of msg-gate is retrieved through channelid (socketid). Receiving a client message is easy to understand, and the corresponding socket triggers an event; if the logic requests to send a message, you also need to provide a channelid (described in the session routing section of the Redis set).

How to get uid (user id) data in session?

Take a look at the client to msg-gate protocol

From the performance point of view, the msg-gate module only parses the message header of the protocol (the first 20 bytes), and the msg-gate module cannot get the user information directly through the protocol. However, the client of each connection to the msg-gate requires legal authorization to send and receive messages, and through this step, msg-gate can associate the uid with the channelid.

2. Session structure in Redis

The session in redis mainly uses msg-logic as the object and the module that handles business logic. The basic structure of session is (structure), in which the session content includes msg-gate address, channelid, etc. (msg-gate and chanelid are also obtained through the authorization process).

Taking uid as the key structure is very consistent with the business logic of msg-logic processing.

3. The conversion relationship between two session

When msg-gate calls the interface of msg-logic, it always converts channelId into uid;msg-logic calling msg-gate 's interface and uid into channelid. Due to the magic hash algorithm, the time complexity of the conversion process is constant 1.

So far, each module of the IM system can efficiently locate the location and status of a client.

The above is what the SESSION structure of the IM system shared by the editor is. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report