O
O
Ockonal2012-04-20 13:51:49
Nginx
Ockonal, 2012-04-20 13:51:49

Subscribers in nginx

Hello. There is a good module: nginx http push module . It allows you to implement long-poll.

The essence of the work is simple: there is an address for sending events to some channel, there is an address for reading an event from the specified channel.

To publish a channel, I make a POST request like this: Answer: Everything is fine, but now I want the client to read this channel. The problem is that the server terminates the GET request. That is, I do: I get a response:

> POST /pub?channel=1 HTTP/1.1
> User-Agent: test
> Host: localhost
> Accept: */*
> Content-Length: 4
> Content-Type: application/x-www-form-urlencoded
>
TEST * upload completely sent off: 4 out of 4 bytes



< HTTP/1.1 202 Accepted
< Server: nginx
< Date: Fri, 20 Apr 2012 10:29:44 GMT
< Content-Type: text/plain
< Content-Length: 81
< Connection: keep-alive
Тут текст о том, что сообщение добавлено



> GET /sub?channel=1 HTTP/1.1
> User-Agent: test
> Host: localhost
> Accept: */*



< HTTP/1.1 200 OK
< Server: nginx
< Date: Fri, 20 Apr 2012 10:29:34 GMT
< Content-Type: application/x-www-form-urlencoded
< Content-Length: 4
< Last-Modified: Fri, 20 Apr 2012 10:29:34 GMT
< Connection: keep-alive
< Etag: 0
< Vary: If-None-Match, If-Modified-Since
<
* Connection #0 to host localhost left intact
TEST* Closing connection #0


So, I don't like "Closing connection". Is it correct that the client/subscriber sends a GET request, gets the last message and disconnects? Before that, I used the nginx push stream module, the connection hung there and the client sent a GET request once and then only read incoming connections from the channel from the socket. Maybe I'm doing something wrong?

The documentation says that you can control this through the Etag, If-None-Match, If-Modifief-Since headers, but they make sense if you need to make a GET request every time to use them. The requests are sent very frequently, so I can't constantly re-create the connection between the client and the server.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
ertaquo, 2012-04-20
@Ockonal

Everything seems to be correct. You pass the header in your POST request:

> Content-Length: 4

Accordingly, nginx takes only the first four letters - "TEST".

A
Anatoly, 2012-04-20
@taliban

javascript.ru/ajax/comet/long-poll Everything is correct, with long requests, the connection is terminated when the response is read, and the request is sent again. The difference between regular requests is only in the response time.
In contrast to the stream, where the socket is opened and the data is driven back and forth without disconnecting (websocket).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question