Home > Article > Backend Development > Nginx implements long connection application
Whether you are doing web backend, app backend, or SOA service, long connection is a good choice. On the one hand, it saves the resource consumption of establishing a connection every time. On the other hand, it allows timely response to messages. Improved experience.
Here is a way to implement long connections through the Nginx module. This method is a long connection on the http protocol. Strictly speaking, the http protocol itself is a request-response type, and there is no strict long connection. The so-called long connection This means that when there is no response, you can hold it until there is a response, and then immediately re-establish the connection.
Let’s talk about how to achieve it.
1. First download NGiNX_HTTP_Push_Module and Nginx, these two tar files;
2. Copy these two tar files to the linux system and execute them in the nginx directory:
./configure --add-module=path/to/nginx_http_push_module ... make make install
4. Wait for nginx to be installed. Finally, configure a long connection:
In the nginx.conf file
in /use/local/nginx/conf
Add:
location /publish { set $push_channel_id $arg_id; push_publisher; push_store_messages on; push_message_timeout 2h; push_max_message_buffer_length 10; } location /activity { push_subscriber; set $push_channel_id $arg_id; push_subscriber_concurrency broadcast; default_type text/plain; }
public void testNginx(){ String http = "http://172.16.4.108/publish?id=my"; PostMethod postMethod = new PostMethod(http); RequestEntity requestEntity = new StringRequestEntity("444"); postMethod.setRequestEntity(requestEntity); try{ int status =this.client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { String text = postMethod.getResponseBodyAsString(); System.out.println(text); } }catch (Exception e){ e.printStackTrace(); } }
The following is the listening end. Here is a simple implementation. We need to always record a lastModified on the listening end. This time represents The time of the last new message he received
private static String etag = ""; private static String lastModified = ""; public static void main(String[] args){ while (true) { HttpClient httpClient = new HttpClient(); String http = "http://172.16.4.108/activity?id=my"; GetMethod getMethod = new GetMethod(http); getMethod.setRequestHeader("Connection","keep-alive"); getMethod.setRequestHeader("If-None-Match", etag); getMethod.setRequestHeader("If-Modified-Since", lastModified); try { int status = httpClient.executeMethod(getMethod); if(getMethod.getResponseHeader("Etag") != null) { etag = getMethod.getResponseHeader("Etag").getValue(); } if(getMethod.getResponseHeader("Last-Modified") != null) { lastModified = getMethod.getResponseHeader("Last-Modified").getValue(); } System.out.println("etag=" + etag + ";lastmodif=" + lastModified + ";status=" + status); if (status == HttpStatus.SC_OK) { String text = getMethod.getResponseBodyAsString(); System.out.println(text); } } catch (Exception e) { e.printStackTrace(); } } }
public void testNginx(){ String http = "http://172.16.4.108/publish?id=my"; PostMethod postMethod = new PostMethod(http); RequestEntity requestEntity = new StringRequestEntity("444"); postMethod.setRequestEntity(requestEntity); try{ int status =this.client.executeMethod(postMethod); if (status == HttpStatus.SC_OK) { String text = postMethod.getResponseBodyAsString(); System.out.println(text); } }catch (Exception e){ e.printStackTrace(); } }
The above has introduced Nginx to implement long connection applications, including aspects of the application. I hope it will be helpful to friends who are interested in PHP tutorials.
🎜