Mobile Internet Message Push principle: Long connection + heartbeat mechanism (MQTT protocol)

Source: Internet
Author: User

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 to maintain long connections:

Before writing, we first understand why Android maintenance long connection needs heartbeat mechanism, first we know that maintaining any long connection requires a heartbeat mechanism, the client sends a heartbeat to

Server, the server gives the client a heartbeat response, thus forming a complete handshake for the client server, this handshake is to let both sides know that the connection between them is not disconnected, the client is online

Of If a threshold of more than one time is exceeded, the client does not receive a response from the server, or the server does not receive a heartbeat from the client, then the client disconnects from the server and re-establishes a

Connection, just disconnect this connection for the server. So what's the difference between long-connected heartbeats on smartphones and long-connected heartbeats on the Internet? The reason is that smartphones use

is a mobile wireless network, we should first understand the characteristics of wireless mobile network before we talk about long connection.

    1. Features of Wireless mobile network

When a smart phone connected to the mobile network, in fact, is not really connected to the Internet, the carrier assigned to the phone's IP is actually the operator's intranet IP, mobile phone terminal to connect to the Internet must also be operated

Gateway for IP address conversion, the 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 network IP, port, IP between the outside network

mapping to each other. 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 functionality, as most mobile wireless network operators reduce the load on the Gateway NAT mapping table, such as

If a link has a period of time without communication will delete its corresponding table, resulting in link interruption, it is this deliberately shorten the release of idle connection time-out, originally wanted to save the channel resources, did not expect to let the Internet

Application must not send a heartbeat beyond the normal frequency to maintain the long connection of the push. This is also why there are rumors of previous signaling storms that shake charges, because this kind of application sends the heartbeat frequency is very short,

Not only caused the waste of channel resources, but also caused the rapid consumption of mobile phone power.

 2. What is the difference between push and iOS push for Android?

First we must know that all push functions must have a long connection between the client and the server, because push is initiated by the server to send a message to the client, if there is no one between the client and the server

A long connection then the server is unable to actively connect to 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 push messages first

To Apple's server and then push the Apple server through this system-level long link to the phone terminal, a few of the benefits are: 1. Always maintain a long connection at the phone terminal, and as

This long link is a system-level condition that does not appear to be killed and cannot be pushed. 2. Power saving, does not appear each application maintains its own long connection. 3. Security, only developers registered with Apple can

Enough to 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, and cloud push, but because Google's servers are not

Country, for other reasons you know. So the push can't be used, Android developers have to maintain a long link on their own, so if each application is 24 hours online, then the respective dimensions

Protection of a long connection, the consumption of electricity and flow 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 push is implemented

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. Third way because operators are not free to open

This signaling has led to the cost of this push is unacceptable, although the way the push is very stable, efficient and timely.

Please refer to this link if you want to learn about the various push modes in Android: http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378971.html This blog has been introduced very well.

Transferred from: http://blog.csdn.net/clh604/article/details/20167263#comments

Mobile Internet Message Push principle: Long connection + heartbeat mechanism (MQTT protocol)

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.