# Nginx+HTTPS event stream issues

**URL:** <https://www.ruby-forum.com/t/nginx-https-event-stream-issues/220048>\
**Category:** NGINX\
**Created:** [May 25, 2012, 4:48pm UTC](https://www.ruby-forum.com/t/nginx-https-event-stream-issues/220048 "2012-05-25T16:48:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 25, 2012, 4:48pm UTC](https://www.ruby-forum.com/t/nginx-https-event-stream-issues/220048/1 "2012-05-25T16:48:29Z")

</div>

Hey guys,

first of all, the stats: running Ubuntu 11.10, nginx 1.0.5 (package  
installed) and PHP 5.3.6 (package php-fpm).

So I have this HTML5 event stream that every 0,5 seconds does some  
database operation and sends events to the browser (which the javascript  
then handles). Everything was working alright, but then I noticed the  
events were getting bundled (3 or 4 of them together) and sent all at  
the same time. Basically, I’d see an event coming, then after waiting 2  
or 3 seconds, I’d see 4 or 5 arrive at the same time. As the idea here  
is a real-time stream, this is definitely not good - the events should  
be sent to the browser as soon as they are created (ie. every 0,5  
seconds - plus some processing time for DB access etc).

So I started playing around with the buffer size. I noticed that my  
events are always roughly 800 bytes, so I used the following parameters:

[php.ini]  
output\_buffering = Off

[nginx site file]  
fastcgi\_buffer\_size 500;  
fastcgi\_buffers 2 900;  
fastcgi\_busy\_buffers\_size 900;  
fastcgi\_max\_temp\_file\_size 0;  
proxy\_buffering off;

With these settings, everything works great, I see the events arriving  
in order, all of them individually, and it looks great.  
But then, of course, everything got wrong again when I started using  
HTTPS. After a lot of nginx debugging (including looking at its source  
code), I realized that nginx buffers the data sent to OpenSSL, and that  
this buffer is 16k bytes. Since I couldn’t find any config options to  
change/disable this SSL buffer, I decided to pad my events with  
whitespaces, in order for them to be roughly 16k long, and set the  
buffers to:

fastcgi\_buffer\_size 17000;  
fastcgi\_buffers 2 17000;  
fastcgi\_busy\_buffers\_size 17000;

Unfortunately, that doesn’t work either: looking at the nginx debug log,  
I see that the events arrive at nginx fine, the buffer is just big  
enough for them, and for every event, nginx writes it immediately to  
SSL:

SSL buf copy: 6  
SSL buf copy: 15974  
SSL to write: 16384  
SSL\_write: 16384

Yet the javascript only sees the first event (confirmed with  
console.log)

Basically, I’d love to have some help from you guys to solve my problem.  
All I need is a real-time event stream in HTTPS. I already tried dozens  
of buffer size combinations, either really small (so that’s nothing is  
buffered) or values around 800bytes (+headers), or values around 16000  
with padding. I also tried compiling nginx from source with  
NGX\_SSL\_BUFFERED (source/core/ngx\_connection.h) set to 0x00 , but that  
didn’t help… Is there any way to just disable SSSL buffering all  
together?!

I am attaching the nginx debug log, hopefully it will make sense to  
someone! (Tip: in the php script, I error\_log “Sending event with  
\<num\_bytes\> bytes” before echo’ing the event output and error\_log  
“SENT!” after - these show up in the log after the “FastCGI sent in  
stderr” messages)

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![Daniel\_Gomes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3d9bf3/32.png) [@Daniel\_Gomes](https://www.ruby-forum.com/u/Daniel_Gomes)\
**Post date:** [May 30, 2012, 11:06am UTC](https://www.ruby-forum.com/t/nginx-https-event-stream-issues/220048/2 "2012-05-30T11:06:16Z")

</div>

Thanks mate, your module does seem pretty handy! I’m not sure it’s what  
I need though,let me explain:

Basically, what I have now (without using the push stream module) is a  
PHP function that, _when called_, pretty much echos the contents of a  
certain DB table (chosen via POST paramters) every 0,5 seconds. On the  
client-side, there is javascript code that sets up an event source (with  
HTML5’s EventSource class) to this PHP script URL and shows the  
information to the user, when the events arrive. Like I said in my  
initial message, this works fine for HTTP, but the SSL buffering means  
the events get bundled together and instead of waiting 0,5secs for an  
event, I only see events after like 10 seconds (and then only the last  
event’s info is seen by the user).

From what I can understand from your module, the publisher is something  
that is always running, ie. always posting new stuff to _all clients_.  
What I need, on the other hand, is an individual channel that is  
different for _each different client_ and each _different DB-information  
to obtain_ (ie. info from what table to post, the fields to return,  
etc). Ignoring how setting up individual channels would be  
time-consuming, what I think is the main problem here is that I would  
have to (client-side) somehow call a PHP function (via XHR, I guess) to  
initiate the proper channel with some parameters (DB table, etc) and  
then go subscribe to it (on a different address). Now, this doesn’t look  
like a good set-up, does it? 😕

In the mean time, I figured out how to disable SSL buffering (or change  
its size), so I might just go with that\*… Would still like to hear  
your opinion, tho, since using a proper publisher-subscriber module  
seems like a better idea!

\*: for anyone with the same problem, its all in  
src/event/ngx\_event\_openssl.h . The buffer size is in NGX\_SSL\_BUFSIZE  
and if you want to disable it completely, change NGX\_SSL\_BUFFER to 0.

Cheers,
