Introduction: if you want to implement a mobile internet IM app on your own, what do you have to do? The first issue to be addressed is the design of the IM protocol. This article will talk about how to design a private tcp protocol from 0 to 1.
Although a variety of messaging push SDKs such as carrier pigeons already exist on the market now, they may not fully meet the demand for various reasons, or you may want to implement an IM or push function yourself. So what issues do you need to address? The first problem faced is how to implement the IM protocol?
Transmission protocol selection
The transport protocols are generally referred to as TCP and UDP protocols. The UDP protocol is connectionless, message-oriented, and provides primarily efficient services. It is efficient and takes up fewer resources, but its transmission is unreliable and only sends, regardless of whether the other party receives it, although reliability can be achieved by other means. TCP is connection-oriented, flow-oriented, and primarily provides reliability services. Reliability is exactly the feature IM needs most, so nowadays mainstream IM is basically implemented using TCP protocol.
Regarding the issue of PC QQ still using UDP, I understand privately that it is due to historical reasons, so it has been used until now. I guess it should be because the C10K problem was not well solved back then, because TCP is connection-oriented, there was no epoll technology existed at that time, and it could not solve the high load problem of simultaneous online well, so we can only use UDP, because UDP is connectionless, there is no load problem, but UDP is not reliable, so we can only implement TCP timeout, retransmission, confirmation and other mechanisms on UDP.
Protocol format selection
commonTCP Protocol formats are usually3 kind： Text Protocol、 Binary protocol、 XML protocol。
Text Protocol It is usually made up of a string ofACSII Data consisting of characters。 Text Protocol Easily interpreted by humans， More suitable for the public， typical ofHTTP agreements。 Cite one.HTTP GET examples of：
Text Protocol characteristics of the： a. Good readability， Easy to develop and debug； b. Good scalability，key-value Easy to extend； c. Better parsing efficiency； d. Smaller flow rate。
The once dominant party ofIM productsMSN The use of is is Text Protocol。
One of the mainstream IM protocols, XMPP, is an open XML-based real-time communication protocol. As an example of an XMPP sent message.
<message from="sendinguser@somedomain" to="recipient@somedomain" xml:lang='en'>
Body of message
XML protocol characteristics of the： a. InheritedXML merits， Good readability， Good scalability； b. Higher cost of resolution， inefficient， Takes up a lot of resources； c. High flow rate。
GTalk, the IM product from Google, uses the XMPP protocol.
Binary protocol It's a stream of bytes.， Generally includes a fixed-length head and an extendable, variable-length body， typical ofMQTT agreements。 Cite one. Binary protocol examples：