Source: http://blog.csdn.net/unityoxb/article/details/8532028
The push service is a useful technology that improves interaction and improves the user experience. There are usually two ways to implement this service, polling and long connections. Polling is when the client asks the server for new data every once in a while, it is very simple to implement but the server is under a lot of pressure, and most requests are wasted because there is no new data. A long connection is when the server suspends a request, does not output anything, and does not complete the request until new data has been generated, and the browser receives a response and then sends another one and hangs the server again and again. The advantage of this is that it can save a lot of useless requests, but it can not use the traditional server-side software, such as Apache and PHP-FPM, the client is a lot easier to take all the process of light, so that servers can not respond to new requests.
node. js makes this simple, and it's a server technology based on event and non-blocking I/O that can respond to a large number of concurrent requests with minimal resources, making it ideal for long-connection requirements. But there are two problems with this. First of all, your server is usually in a different set of languages and frameworks, with mature code and business logic, in order to achieve this push function, do you want to use JavaScript to write a set? Not too much trouble to maintain? Second, the service side to suspend the request, but also repeatedly call other services to obtain new data, this is just the polling code in a different location, essentially no difference, as the server pressure.
Is there any simple way to achieve efficient push?
The answer is yes, and it's simple, the code needs no more than 20 lines!
First we use Redis's pub/sub function for real push, followed by JSON as the data format for client and server communication.
When node. JS receives the request, we suspend the request, instantiate a Redis client, and listen to a specific channel based on the parameters in the request, and the original service-side code (such as PHP), after processing the business logic, publishes the new data to the channel in a JSON package, and node. JS receives the message and passes it as a response To the client, so that a push is completed. The code is as follows:
[JavaScript]View Plaincopy
- var http = require (' http ');
- var url = require (' url ');
- var Redis = require (' redis ');
- Http.createserver (function (req, res) {
- var query = Url.parse (Req.url, true). Query;
- if (typeof query.channel = = ' undefined ') {
- Res.writehead ($, {' content-type ': ' Text/plain '});
- Res.end (' Invalid request\n ');
- }else{
- var client = redis.createclient (6379, ' 127.0.0.1 ');
- Client.subscribe (Query.channel);
- Client.on (' message ', function (channel, message) {
- Res.writehead ($, {' content-type ': ' Application/json '});
- Res.end (message);
- Client.unsubscribe ();
- Client.end ();
- });
- }
- }). Listen (1337, ' 127.0.0.1 ');
This method is easy to use, you only need to set a channel and data relationship protocol, and then make some simple changes to the existing code, you can achieve an efficient push service!
Implementing push services with node. js and Redis-Reproduced