Are there any languages or even servers that instead of running a script on page load, (Apache/PHP, Ruby on Rails, …) actually keep the code loaded and running forever, and when it receives a connection it calls a function of that code? (Like a PageLoad event basically?)
I didn’t really know how to call this so I went for Passive Web Server because the code is basically sleeping till an event occurs, instead of fully unloaded or just cached.
5
PHP and scripts run through CGI (not FastCGI) are about the only things that work this way. Almost everything else has a long-lived process that runs & responds to requests. A typical web application will have a static server (Apache, Nginx…) in front, handling client requests directly. For static files (Javascript, CSS and so on), it’ll just send the file; for things that require your app to respond, it will pass the request on to your application & then forward the response to the client.
One example would be a simple Python/Django install. We have Apache running on the server. As part of our Apache configuration, we’re running mod_wsgi. The mod_wsgi module allows Apache to fire up a number of Python interpreters that have long-running jobs in them. When a request comes in, Apache sees that it should go to Python & hands the request over to mod_wsgi and mod_wsgi passes the request into your already loaded & running Django application.
Another example would be a Ruby on Rails application running behind Nginx and Mongrel. Mongrel is a web server that lets Ruby applications live inside of it. It’s really good at serving dynamic pages but it’s a little slow at static stuff, so we set up Nginx for that. Nginx serves the static content (like Apache did in the last example) but whenever a request for one of the dynamic pages comes in, Nginx just acts as a proxy and forwards the request to Mongrel.
There’s a minor difference between the two cases but it’s not really worth delving into unless you’re handling server architecture for a high-volume website.
A third setup comes in with Node.js. With Node, the web server is your application.
Node.js scripts typically work that way. They act as their own web server, essentially, but are a single process that stay loaded between client requests. This is why its so good at ‘real time’ communication – it can stack and manage multiple connections and pass data between them.
Python through WSGI with Apache stays loaded. Frameworks like Django route the call to the appropriate module / class.