Raghav Tandon
Raghav Tandon

Reputation: 183

Apache , NodeJs, Server Sent Events

First of all, I know (Putting Node under Apache) this is not the approach to go but due to time constraint I cannot experiment.
I am trying to use Server Sent events for a mobile application. After reading all over Net, I figured out that Nodejs is the server to go for. My 80/443 port is occupied by Apache Web server, so I want Node To run behind Apache.

The Problems which I am facing are:-

1. I am not able to get the close/end events on refreshing browser or closing browser rather I get after a fixed certain amount of delay, so not able to maintain when the client shuts down connection in Nodejs.

req.on("close", function() {
    removeConnection(res);
    console.log('Connection closed');
});


2. Apache is sending Keep-Alive:timeout=5, max=100 which I dont want as I want client to be connected forever till anyone closes connection, due to this Browser automatically closes connection and I start getting net::ERR_INCOMPLETE_CHUNKED_ENCODING. How Can I modify this value only for Node Proxy Requests.

I have added
ProxyPass /events http://localhost:5000/events
ProxyPassReverse /events http://localhost:5000/events

Response Headers

Access-Control-Allow-Headers:key,origin, x-requested-with, content- type,Accept,Content-Type
Access-Control-Allow-Methods:PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Connection:Keep-Alive
Content-Type:text/event-stream; charset=utf-8
custom:header
Date:Wed, 13 Jan 2016 18:12:48 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)
Transfer-Encoding:chunked
X-Powered-By:Express

Note:- All this is happening when I am using Apache to proxy to Node, else if I directly hit Node (which I cannot in prod due to blocked port) everything works fine.

Upvotes: 1

Views: 1612

Answers (1)

voji
voji

Reputation: 493

The KeepAlive settings by default allowed only in server or virtual host configuration.

But during the request processing, apache2 use environment variables (based on apache configuration) to determine the current settings.

Fortunately with mod_rewrite, you can alter apache environment variables on request basis, so you can disable keepalive on specific request.

For example:

#Load rewrite module
LoadModule rewrite_module modules/mod_rewrite.so

#Enable mod_rewrite functionality
RewriteEngine On

#rewrite rule inside locationmatch
<LocationMatch ".*\/sse\/.*">
   #This is not required, used only for debug purposes
   Header set X-Intelligence "KEEPALIVEOFF"       
   #Here goes the mod rewrite environment variable trick
   RewriteRule .* - [E=nokeepalive:1]
</LocationMatch>

Upvotes: 0

Related Questions