"Instant messaging technology analysis and practical" study notes 2-- allows users to chat peer messaging architecture

Most of message storage server, for ease of viewing history information or for temporary storage of offline messages.
A user support peer chat messaging architecture includes three parts: a message storage, and unread message messaging channel.

A message storage

Assuming that the sender and receiver of the message history are independent of each other, that is, one party after sending a message to delete the message, the other can still get to this message, then the message is stored need to use two tables: message content table (figure user_message table) and message index table (user_message_history table in the figure). The former is mainly stored message ID, message content, message type, message creation time, etc., which can be understood as chat history, recording the sending and receiving sides of the user ID, ID and a former associate through messages.
General IM system also requires a recent list of contacts (user_message_contacter table in the figure), so that both sides can quickly find interactive chat with need, contact lists will carry a chat message for the two most recent show, the table with the message index table difference is that: the message index table stores the sender and receiver of the message history logs, contacts list is mainly used to query a user's most recent all contacts.

Suppose Joe Smith sends a message to John Doe, inserts a data (assumed to be the table in FIG. User_message 1001 record ID) to your message table, and then inserted into the two message data to the index table, a user ID is Zhang recording three (assuming the table in FIG. user_message_history ID 30923 of the record), a user ID to the message ID is John Doe's record (assuming FIG user_message_history table ID 30922 of the record), 1001 are two records.
And it will update Joe Smith's recent contacts respectively of recent contacts and John Doe, the former is to find whether there is a contact list for the user ID USERID Joe Smith and John Doe interact with people with ID USERID record, if not, insert a the new contact record, the latest message ID is 1001; the other hand, if you have had chats before Joe Smith and John Doe, you can update the latest message ID. The same way John Doe update of recent contacts.

If you want to list Joe Smith and John Doe dialogue recording a message, you can use the following query SQL statement:

select * from user_message_history where user_id = 张三USERID or contacter_id = 张三USERID;

Second, the unread message

If one sends a message, and the recipient is offline or restrict the notification bar to remind permissions, you need to remind unread as a remedy. It is set to achieve a particular total number of unread messages and a message for the recipient does not read the session.
When Joe Smith John Doe to send a message, the IM server receives the message, not to John Doe's total reading plus 1, John Doe and Joe Smith to the session was not reading plus 1;
after John Doe View this news will not perform readings change, the total John Doe is not to be decremented 1, and the seating of the John Doe reading session is not decremented.
In general, the need to support the "multi-terminal roams message" function, is not stored in the IM server reading, contrary to select local storage.

Third, the messaging channel

  • Send channel

A TCP connection is maintained longer, IM server provides an API to send messages between the client and the IM server;
when the client has sent the message, encapsulates the message in a private protocol, and then call the API service end message to IM .

  • Receive Channel

Maintain a persistent connection between the client and the service gateway server receives a message of an IM (TCP connection or long length Websocket connected), by means of TCP can simultaneously receive data transmission capability, the message from the IM server is pushed to the receiving party. If the recipient is not online (no network or unopened APP), the Department may use third-party operating system other auxiliary channel, the vendor channel various devices, a message through the notification bar pushed to the recipient.

Guess you like

Origin www.cnblogs.com/sunshineliulu/p/11441026.html