DWQA QuestionsCategory: ProgramHow to implement web message push
Evening rain asked 1 month ago
  1. The website should implement a message push function, such as logging into the personal center, there will be a small prompt for push, and clicking on it will push the content of the message
    Using the development language node.js, redis can be used as message queue, and the server can push messages to the client. To use websocket, someone has done similar functions, and can use simple general architecture ideas
2 Answers
Best Answer
troybug answered 1 month ago

You can try itsoket.io
Server programming model:

var io = require('socket.io')(server);

io.sockets.on('connection',function(socket) {
    console.log('socket connection');


Client programming model:

var socket = io();

socket.on('serverMsg',function(data) {
    console.log(data.msg);    //hello
Evening wine answered 1 month ago

In the development of Android and iPhone applications, we often need to push all kinds of notification messages from the server to the mobile client in real time. The iPhone already has a relatively simple
With the perfect push notification solution, but it is relatively troublesome to implement it on Android platform. Recently, we use a few days to conduct preliminary research on the push notification service of Android.
On the Android mobile platform, Google provides the C2DM (cloudto device messaging) service. At first, I was going to use this service to implement the push function on my mobile phone.
Android Cloud to Device Messaging
(C2DM) is a service used to help developers send data from the server to Android applications. The service provides a simple, lightweight mechanism that allows servers to notify mobile applications
The program communicates directly with the server to obtain application updates and user data from the server. The C2DM service handles transactions such as message queuing and distributes these to applications running on the target device
However, after some research, it is found that there are many problems in this service:
1) C2DM is built into Android 2.2 system, and cannot be compatible with the old 1.6-2.1 system;
2) C2DM needs to rely on the official C2DM server provided by Google. Due to the domestic network environment, this service is often unavailable. If we want to use it well, our app server must also be abroad, which may not be possible for every developer;
With the above two restrictions on use, I finally gave up this scheme, but I want to use another article to introduce the C2DM framework and the corresponding setting methods of client and app server in detail, which can be used for learning and reference.
That is to say, C2DM can’t meet our requirements, so we need to realize the communication protocol between Android mobile client and app server by ourselves to ensure that when app server wants to send messages to the specified Android device, Android device can receive them in time. Here are some common solutions:
1) Polling: the application should periodically connect to the server and check whether new messages arrive. You must communicate with the server by yourself, such as message queuing. And you have to consider the frequency of polling. If it’s too slow, it may cause some messages to delay. If it’s too fast, it will consume a lot of network bandwidth and battery.
2) SMS: on Android platform, you can understand the intention of the server by intercepting SMS messages and parsing the message content. It’s a good idea. I’ve seen it adopted
Application. The advantage of this scheme is that it can realize complete real-time operation. But the problem is that the cost of this scheme is relatively high. It is difficult for you to find a free SMS gateway. For the implementation of this scheme, you can
Refer to the following link:https://labs.ericsson.com/api…。
3) Persistent connection: this solution can solve the performance problem caused by polling, but it still consumes the battery of the mobile phone. Apple’s push service works well because every mobile phone only protects
Keep a connection with the server. In fact, C2DM works the same way. However, there are some shortcomings in this scheme, that is, it is difficult for us to achieve a reliable service on the mobile phone. Android operating system
The system service may be killed in low memory, so your notification service is likely to be killed by the operating system.
There are obvious deficiencies in the first two schemes and the third scheme, but we can make up for them through good design so that the scheme can work effectively. After all, we need to know that Gmail, Gtalk and google voice can be updated in real time.
Ø Android push using mqtt protocol
Mqtt is a lightweight message publish / subscribe protocol, which is an ideal solution to realize the message push server based on mobile client.
We can download the instance code of this project from here, and we can find a server-side implementation written in PHP.
The architecture is as follows:
Wmqtt.jar is an implementation of the mqtt protocol provided by IBM. You can download it from the following site. You can add the jar package to your own Android application.
Really small message broker (rsmb), a simple mqtt agent, is also provided by IBM. Port 1883 is opened by default. In the application, it is responsible for receiving messages from the server and forwarding them to the specified mobile device.
Sam is a PHP library written for mqtt. You can download it from this
Send_mqtt.php is a PHP script that receives messages through post and sends them to rsmb through Sam.
Instance code:
You can download the instance application from GitHub. After running the app, access it through mobile browserhttp://tokudu.com/demo/androi…Enter the device ID in the first input box, the message content you want to send in the second input box, and press the “send push message” button, you should be able to see the notification received on the mobile phone. You can also download the Android push source code from the GitHub address, which contains the send_mqtt.php script.
Ø using XMPP protocol to implement Android push
This is the plan I used in the project. In fact, the bottom layer of Google’s official C2DM server is also packaged with XMPP protocol.
XMPP (extensible communication and presentation protocol) is a protocol based on extensible markup language (XML), which is used for instant messaging (IM) and online detection. This protocol may eventually allow Internet users to send instant messages to anyone else on the Internet.
Android PN is a Java open source Android push notification implementation based on XMPP protocol. It includes the complete client and server side. After the source code research, I found that the server-side is basically modified and implemented on the basis of another open source project, openfire. However, it is more depressing that Android PN documents are written in Korean, so the whole research process is basically reading the source code. Its implementation diagram is as follows:
Android PN client needs to use a Java based open source XMPP protocol package asmack, which is also based on another open source project smack under openfire,
However, we do not need to compile by ourselves. We can directly use the asmack.jar in the Android PN client. The client uses the
The xmppconnection class establishes a persistent connection with the server, through which users can register and log in for authentication. It also receives notifications sent by the server through this connection.
Android PN server is also implemented in Java language, based on openfire open source project, but its web part adopts spring framework, which is similar to
Openfire is different. The Android PN server consists of two parts. One is to listen to the XMPP service on port 5222, which is responsible for the connection with the client
The xmppconnection class communicates to register and authenticate users and send push notification messages. The other part is web server, which uses a lightweight HTTP server,
Responsible for receiving web requests from users. The server architecture is as follows:
The top layer consists of four parts, session manager and auth
Manager, presencemanager, and notification
Manager. Session manager is responsible for managing the session between client and server, auth manager is responsible for client user authentication management, presence
Manager is responsible for managing the login status of client users, and notification manager is responsible for pushing messages from server to client.
The server-side interface is as follows, corresponding to the above functional modules:
After sending, we can see the received message on the mobile terminal:
The biggest advantage of this solution is simplicity. We don’t need to rely on the operating system version like C2DM, nor worry about the unavailability of Google server one day. With XMPP protocol, we can further expand the protocol and realize more perfect functions.
With this scheme, we can only send text messages at present, but generally enough for push, because we can’t expect to get all the data through push. In general, using push only tells the mobile server that some changes have taken place. When the client receives the notice, it should take the initiative to get the latest data from the server, which is the push service Complete implementation of services.