Today I read the high scalability article aimed to reduce the amount of requests to your server by not sending XMLHttpRequests for every keystroke.
It was suggested that a onblur event was used to measure when the user had stopped typing.
Will your users expect to have to tab out of a field before something else happens? Will it confuse them if they've started typing in another text area? Probably.
At work, I'd whipped up something elegant to handle this very same paradigm for validating or searching user input.
Example
(using jquery to make those events really easy)
$('input.search').keyup(function () {
// Lets have a clean version of the search string, you could really do more there though
var search_term = new RegExp(this.value.replace(/\\/g, ''), "i");
var search_execution = function () {
// Expensive server queries here - buy some real servers guys! (what on earth are you requesting?)
}.sleep(175);
});
175 milliseconds delay before the method is executed is a good typing rate. Maybe you want to change this to something that you're more comfortable with though.
Hold on, last time I checked, sleep() wasn't a part of the standard JavaScript language.
Rather than using a simple setTimeout(), I decided that the ajax'd method shouldn't be executed again until it had finished the last time it ran.
The sleep method
Function.prototype.sleep = function (millisecond_delay) {
if(window.sleep_delay != undefined) clearTimeout(window.sleep_delay);
var function_object = this;
window.sleep_delay = setTimeout(function_object, millisecond_delay);
};
er, yeah.. thats about it. I just really wanted to post the sleep method. I'd written it over a year ago now, but it's still used on a pretty regular basis. I use it for searching conducted in the DOM by hiding elements that don't match a simple regex.
Tags:server
September 19, 2007 · 2 comments
Like everyone else, I read ezra's nginx config and put it straight to use.
After recently becoming addicted to yslow, tweaking configs day and night (well, it happened once. yslow is great though) I decided to add expires headers to anything that isn't served by rails, your js, css, images etc.
Without further ado, my config:
worker_processes 2;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx_access.log main;
error_log /var/log/nginx_error.log debug;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzip on;
gzip_http_version 1.0;
# More is heavier on the CPU
gzip_comp_level 5;
gzip_proxied any;
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
upstream schwarz {
server 127.0.0.1:4000;
server 127.0.0.1:4001;
server 127.0.0.1:4002;
}
server {
listen 80;
client_max_body_size 50M;
server_name .germanforblack.com;
root /var/www/public;
access_log /var/log/nginx.vhost.access.log main;
location / {
proxy_set_header X-Real-IP $remote_addr;
# needed for HTTPS
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
proxy_max_temp_file_size 0;
if (-f $request_filename) {
expires max;
break;
}
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
# Tasty caching
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://schwarz;
break;
}
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/public;
}
}
}
Note the expires method. You just sped up your app dramatically
Tags:server
September 18, 2007 · 0 comments
Write the following into your /etc/lighttpd/lighttpd.conf
server.bind = "0.0.0.0"
server.port = 3000
server.document-root = CWD
server.dir-listing = "enable"
mimetype.assign = (
".css" => "text/css",
".gif" => "image/gif",
".htm" => "text/html",
".html" => "text/html",
".jpeg" => "image/jpeg",
".jpg" => "image/jpeg",
".js" => "text/javascript",
".png" => "image/png",
".swf" => "application/x-shockwave-flash",
".txt" => "text/plain"
)
Not bad, now, a really nice way to execute this, would be from the directory of where your sites' html & assets are at.
Place the folowing in to your ~/.bash_login or ~/.profile file.
alias li="lighttpd -D -f /etc/lighttpd/lighttpd.conf"
Proof is in the pudding:
bens-pb:~/sites ben$ li
2007-09-18 23:44:55: (log.c.75) server started
Ctrl+c will quit the server
2007-09-18 23:45:30: (server.c.1216) [note] graceful shutdown started
2007-09-18 23:45:30: (log.c.135) server stopped
Shazam! Easy.
Tags:server