Implementing push services with node. js and Redis-Reproduced

Source: Internet
Author: User

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
  1. var http = require (' http ');
  2. var url = require (' url ');
  3. var Redis = require (' redis ');
  4. Http.createserver (function (req, res) {
  5. var query = Url.parse (Req.url, true). Query;
  6. if (typeof query.channel = = ' undefined ') {
  7. Res.writehead ($, {' content-type ': ' Text/plain '});
  8. Res.end (' Invalid request\n ');
  9. }else{
  10. var client = redis.createclient (6379, ' 127.0.0.1 ');
  11. Client.subscribe (Query.channel);
  12. Client.on (' message ', function (channel, message) {
  13. Res.writehead ($, {' content-type ': ' Application/json '});
  14. Res.end (message);
  15. Client.unsubscribe ();
  16. Client.end ();
  17. });
  18. }
  19. }). 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

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.