Protocol Selection
What network transmission protocol (TCP/UDP/HTTP) to use?
- Although the udp protocol has better real-time performance, how to deal with safe and reliable transmission and message interaction between different clients is a difficult problem, and it is too complicated to implement. At present, most IM architectures do not use UDP to implement.
- HTTP
HTTP can be used to implement the status protocol, and the pull mode is used for offline messages to avoid excessive pressure on the tcp channel and affect the efficiency of instant message delivery.
When chatting with pictures via IM: http can easily handle functions such as resuming uploads from breakpoints and segmented uploads. - TCP: maintain long connections, ensure the real-time nature of messages, and correspond to data transmission protocols. Purpose: Send and receive messages in time.
Thinking comprehensively, use http and tcp.
What data communication protocol to choose?
- The principle of IM protocol selection is generally: easy to expand, convenient to cover various business logics, and save traffic at the same time. The need to save traffic is especially important on mobile IM.
xmpp |
The protocol is open source, highly expandable, and has various languages at each end (including the server) The realization of , easy for developers to access. But there are also many disadvantages: XML is weak in expressiveness, there are too many redundant information, the traffic is large, and there are a lot of sinkholes in actual use. |
MQTT |
The protocol is simple and has less traffic, but it was not designed for IM at the beginning Protocol, mostly used in the Internet of Things and push. IM needs to be realized in business by itself. (Many companies have used MQTT to implement a general IM framework in recent years), and the implementation of the server is more complicated than other protocols. |
private agreement |
Implement the protocol by yourself, a well-designed private protocol generally has the following advantages: high efficiency, saving Traffic (generally using binary protocol), high security, difficult to crack. |
Comprehensively, the MQTT protocol is more suitable for us.
MQTT protocol overview
client
Refers to a program or device that uses MQTT. The client always connects to the server. It can:
- Publish application messages that other clients may be interested in
- Subscribe to the application news that you are interested in
- Unsubscribe from app messages
- Disconnect from the server
Server
Acts as a middleman between clients that subscribe to or publish application messages. A server:
- Accept network connections from clients
- Accept application messages published by the client
- Process client subscription and unsubscribe requests
- Forward application messages that match client subscriptions
From the above protocol definition, we know that MQTT limits the client and server to 4 functions. If we want to realize the IM business, we need to realize it according to the requirements stipulated by it.
Example
Scenario: Client A wants to send a message to client B, how is it implemented according to the MQTT protocol.
The simplest process, B subscribes to the server topic test, A publishes a message to the server test topic, and B receives the message.
The flow chart is as follows:
The special way of the MQTT protocol is its publishing and subscription. Compared with the traditional sending and receiving of messages, it requires one more step of subscription. There is no other way to receive messages by subscribing.
Technical Implementation
Before talking about the implementation method, let’s talk about the technical difficulties.
Difficulty 1: The client maintains a stable long-term connection with the server, such as heartbeat strategy, disconnection and reconnection.
Difficulty 2: How to ensure that messages are reachable (not lost)/unique (not repeated)/sequential (not out of order).
Difficulty 3: Maintain protocol logic such as publication and subscription required by the mqtt protocol.
Implementation method 1
Self-developed, the server maintains a long connection with the client through netty. Netty is based on the java technology stack. There are also erlang or nodejs implementations on the Internet, but it is recommended to use netty after comprehensive consideration. Subscription publishing can be implemented using middleware such as kafka or rabbitmq.
Advantages of the solution: Self-development is not restricted by the framework, and various businesses can be realized. After the development goes online smoothly, the operation low cost.
Disadvantages of the solution: high development costs, poorer stability than other methods, and many factors to consider , prone to problems.
Method 2
Based on ActiveMq
Directly use the open source activieMQ to implement the client and server.
Advantages of the solution: No need to worry about long-term connection stability, message delivery rate, order, etc. .
Disadvantages of the solution: Based on activeMq, there may be some business restrictions.
Method 3
Use third-party IM services, well-known providers: Ronglian, NetEase Yunxin, Huanxin, Rongyun, etc.
Contrast article reference:
Advantages of the solution: In comparison, the development workload is minimal and the service is stable.
Cons of the plan: Fees.
Key Questions Answered
Is the MQTT protocol practical in IM business scenarios?
Answer: Practical.
How to push?
Answer: If you develop it yourself, you will first determine whether the user is online. If you are online, you will post a message to the user through the long chain, and the client will reply to receive the push message to ensure that the push is successful.
If the user is not online, the internal push system will be called to use Jiguang to push the message to the user.
Protobuf implementation
Answer: The server imports the google jar package to encode and decode protobuf content. The content definition of Protobuf refers to the MQTT protocol specification.
IM Message ID
Answer: The server uses the snowflake algorithm to generate unique and ordered unique message ids. Guarantee the order and uniqueness of messages.
20210321 update
When writing this article, I planned to use the MQTT protocol, and finally, after a meeting and discussion, I used a custom protocol. The client is based on Tencent’s MARS framework, the protocol is customized + protobuf, and the server is based on netty.
The first version of IM is live.