Switch to Internet Push Service principle: Long connection + heartbeat mechanism (MQTT protocol)

Source: Internet
Author: User

http://blog.csdn.net/zhangzeyuaaa/article/details/39028369

Directory (?) [-]

    1. Features of Wireless mobile network
    2. What is the difference between push and iOS push for Android?
    3. How push is implemented
The way the internet pushes messages is very common, especially mobile Internet, mobile phone can receive a lot of push message every day, after research found that the principle of these push service is to maintain a long connection (or can not achieve real-time effect), but the normal socket connection to the server consumption is too large, So there's a lightweight, low-consumption protocol like MQTT to maintain long connections, so how do you maintain long connections?

Before writing, we first understand why Android maintenance long connection needs heartbeat mechanism, first we know that maintenance of any long connection requires a heartbeat mechanism, the client sends a heartbeat to the server, the server to the client a heartbeat response, thus forming a complete handshake of the client server, This handshake is to let both sides know that the connection between them is not disconnected and the client is online. If a threshold of more than one time, the client does not receive a response from the server, or the server does not receive the heartbeat of the client, then the client is disconnected from the server to reestablish a connection to the server as long as the connection can be disconnected. So what's the difference between long-connected heartbeats on smartphones and long-connected heartbeats on the Internet? The reason is that smartphones use mobile wireless networks, so we should first understand the features of wireless mobile networks before we talk about long connections.

1. Wireless mobile network features: When a smart phone connected to the mobile network, in fact, there is no real connection to the Internet, the operator assigned to the phone's IP is actually the operator's intranet IP, mobile phone terminal to connect to the Internet must also be through the operator's gateway IP address conversion, This gateway is referred to as NAT (NetWork Address translation), the simple point is that the mobile phone terminal connection to the Internet is actually the mobile intranet IP, port, the external network IP mapping. It's equivalent to a hole in the mobile wireless network on the phone terminal connected to the outside Internet. The schematic diagram is as follows: (Source network)

The GGSN (Gateway GPRS Support NOTE Gateway GPRS supporting node) module implements the NAT function, because most mobile wireless network operators in order to reduce the load of the Gateway NAT mapping table, if a link has not communicated for a period of time will delete its corresponding table, caused by the link interruption, it is this deliberately shorten the release of idle connection time-out, originally wanted to save the channel resources, did not expect the application of the Internet should not exceed the normal frequency of sending heartbeat to maintain the long connection push. This is also why there will be a previous letter to the storm, wave charges rumors, because this kind of application to send the heartbeat frequency is very short, both caused by the waste of the channel resources, but also caused by the rapid consumption of mobile phone power.

What is the difference between push and iOS push for 2.android systems:

First we must know that all push functions must have a long connection between the client and the server, because the push is initiated by the server to send a message to the client, if there is no long connection between the client and the server then the server is unable to actively connect the client. Thus the push function is based on the long connection basis.

iOS long connections are maintained by the system, which means that Apple's iOS system maintains a long link between the client and Apple servers at the system level, and pushes all apps on iOS to the Apple server and pushes the Apple server through the system-level long link to the phone terminal. A few of the benefits of this are:1. Always maintain a long connection on the phone terminal, and because this long link is at the system level will not be killed and cannot push the situation. 2. power saving, does not appear each application maintains its own long connection. 3. security, only in Apple registered developers can push, and so on.

Android's long connection is maintained by each application, but Google has also launched a push framework similar to Apple's technology architecture, C2DM, Cloud push, but because Google's servers are not in China, other reasons you know. So the push can't be used, Android developers have to maintain a long link, so each application if all 24 hours online, then all have to maintain a long connection, this power and traffic consumption is conceivable. Although a variety of push platforms have appeared in the country, they are unable to achieve the level of maintenance that only a long connection consumes.

3. How to push the implementation:

One: The client constantly query the server, retrieving new content, that is called pull or polling method

Two: A TCP/IP long connection is maintained between the client and the server, and the server push the client

Third: When the server is new content, send a message-like signaling to the client, the client receives the new content from the server, that is, SMS push mode.

Apple's push system and GOOGLEC2DM push system are all in the system level to maintain a TCP/IP long connection, are based on the second way to push. The third way because operators are not free to open this signaling makes this push cost unacceptable, although this push is very stable, efficient and timely.

Switch to Internet Push Service principle: Long connection + heartbeat mechanism (MQTT protocol)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.