Nginx HTTP Push, realtime changes on your website

Author: Eugeniy Marilev
Tags: PHP Linux AJAX
Date of publication: 2014-09-03 19:11:31

We offer Comet ("Long Polling") technology, the basic idea of ​​which is the transmission of information to browser on the initiative of the server. Comet servers implement HTTP Push Relay Protocol. The client connects to the web-server channel with a unique key (makes a usual connection and passes a unique channel key in parameters), and the server keeps the connection until the next portion of data . Sends data to the channel, as a rule, the server itself when a specific event occurs (e.g. a user is logged on-line or the message was sent). After sending the data HTTP-connection is closed or is open to the discretion of the developer. As we can see, the Comet rule does not introduce anything new to the HTTP protocol, as this approach is realized by conventional means.

For the introduction of Comet technology it is proposed to install Nginx-server with nginx_http_push_module module. You need to download the source files and compile Nginx with this module:

  1. Download and unpack the source code of the last stable version of nginx-server and nginx_http_push_module
    	sudo wget
    	tar -xvf nginx_http_push_module-0.692.tar.gz
  2. Also when you may need to install the library zlib, so it is advisable to download it as well
    	tar -xvf nginx_http_push_module-0.692.tar.gz
    	tar -xvf zlib-1.2.5.tar.gz
  3. Go to the folder nginx-1.6.0 and compile
            cd /path/to/nginx-1.6.0/
    	./configure –with-zlib=/path/to/zlib-1.2.5 --add-module=/path/to/nginx_http_push_module-0.692 
    	make install


To work, you need to configure the server. In order to configure this server it is necessary to define two addresses: the publication address and the address of listening to the messages.

location /publish {
	# Defines the publication address
	# The name of the variable that will be recorded unique channel key into
	# in the example "cid", i.e. the request will be:
	set $push_channel_id $arg_cid;
location /listen {	
	# Specifies the subscription address to listen on the channel 
	# Unique channel key, as push_publisher
	set $push_channel_id $arg_cid;
	# Answer’s default type
	default_type text/plain;

Sphere of application

Comet technology is used in various web-applications. As a rule where you want to make some changes to the page of the user in real-time without reloading the page. Such as an on-line chat. Initially it is unknown, at which moment of time one user sends a message another user. If you implement chat using constant ajax-requests in a certain interval, the error will be in time, because the user is aware of the incoming message not when the message was sent, but during the next ajax-request. This does not satisfy the requirements of the chat, but the Comet fully complies with such requirements. Moreover, the technology saves greatly server’s resources. Right now, it becomes more widespread, because web-based applications become more dynamic and user’s interface should react instantly to the server’s changes, and other users' actions.

We offer a NginxHttpPush class for listening and posting in nginx_http_push_module channels. This class is used in popular frameworks yii, if desired, it can be easily transferred to another platform. It consists of two methods.

The first method is publish() - to publish messages that takes on a unique input channel key, data, data type (default application/json), as well as the module response code nginx_http_push_module, which should return ngins server in case the message was sent successfully.

Method listen() - to subscribe to wiretap posts. The channel key is passed in the parameters, as well as a flag, that, if true would listen to the channel till the end and timeout response from the server.

Class' properties are indicated as the general setup of the nginx server, such as the host, address of publication and wiretapping, communications port, the variable name with a unique key and a timeout request.

You can download yii component's sources here.Enjoy!

Article comments
No results found.
Only logged users can leave comments.