Streaming HTTP Response Output in OpenResty
In this video, I’ll demonstrate how to do streaming HTTP response body output in OpenResty.
1 | cd ~/ |
We create the sub-directory structure as always.
1 | mkdir logs conf html |
We write the boilerplate configuration quickly.
1 | vim conf/nginx.conf |
And make the following edits in this file:
- We create an HTTP server listening on the 8080 port.
- Add a
/test
location. - It is important to specify this
application/octet-stream
MIME type to make the Chrome web browser happy. - Add some Lua code via the content_by_lua_block directive.
- We output one line of output every one second.
- We need to call the ngx.flush method explicitly to flush nginx’s write buffers. This is a 100% nonblocking call.
- And we use ngx.sleep to sleep for one second in each loop iteration. This is nonblocking as well.
- Then we create a root location for the
html
directory.
1 | worker_processes 1; |
Let’s check the whole directory tree right now.
1 | tree . |
Looking good.
Now start this OpenResty application.
1 | openresty -p $PWD/ |
Time to query our HTTP location with curl
.
1 | curl 'http://127.0.0.1:8080/test' |
Cool, it is indeed generating one line of per second!
To verify if everything is indeed nonblocking, we can load this HTTP API with the weighttp
utility. Note that it will take a while because we have intentionally slow responses here.
1 | weighttp -c 500 -k -n 500 127.0.0.1:8080/test |
So with 500 concurrent requests, we can still achieve more than 120 requests per second! Note that each request takes 4 seconds to complete. And here we are using only a single worker process and a single operating system thread.
We can still increase the concurrency level a lot. But we will need to tweak the nginx configuration accordingly.
1 | vim conf/nginx.conf |
Like tuning the worker connections to a larger value.
Better performance can also be achieved by enabling the access log buffering. We can also reduce the request memory pool size.
Now let’s create an HTML page to test it out in a web browser.
1 | vim html/a.html |
We make the following edits in this file.
- Add a
DIV
tag to hold the output. - Add some JavaScript to send an AJAX request to our previous HTTP location.
- We fetch out
DIV
element. - Let’s add a JavaScript function to do streaming response receiving.
- Make sure there is new data.
- Append our new data to the
DIV
element. Here we are lazy. We do not bother escaping special HTML characters. - And we check new incoming response data every second.
- Firefox supports a more clever way but we have to support Chrome as well.
- Finally, we handle the end of the response body stream.
- Check the response data for one last time.
- Remove our periodic timer.
- And append the final output to the web page.
- We omit error handling here for brevity.
1 | > |
Let’s check the whole directory tree again.
1 | tree . |
We don’t need to reload or restart the OpenResty server since it is just a static HTML page.
1 | ps aux|grep nginx|grep -v /tmp/ |
Time to point chrome to this HTML page.
It works! This is all I want to cover today.
If you like this tutorial, please subscribe to this blog site and our YouTube channel. Thank you!
About This Article and Associated Video
This article and its associated video are both generated automatically from a simple screenplay file.