Connecting directly to Redis with (client side) javascript? Connecting directly to Redis with (client side) javascript? linux linux

Connecting directly to Redis with (client side) javascript?


You can only make HTTP requests with client-side JavaScript and, in some browsers, websockets. However, you should look into Webdis. It adds an easy HTTP/JSON layer to Redis and should do exactly what you want.

Edit: Link fixed.


The real obstacle is overcoming the non-port 80/443 limitation for the ajax request in the browser; Even with the Webdis solution, because it runs off port 7379 by defaul,t and would conflict with your Apache or Nginx process if ran off port 80.

My advice would be to use the nginx proxy_pass to point to webdis process. You can redirect traffic to port 80 and perform ajax request without the annoying security issues.

Below is a sample NGINX configuration that seems to do the trick for me.

upstream WebdisServerPool {    server 127.0.0.1:7379; #webdis server1    server 192.168.1.1:7379; #webdis server 2}server {    listen   80; #    root /path/to/my/php/code/;    index index.php;    server_name yourServerName.com;    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {            expires max;            log_not_found off;    }    location / {            # Check if a file exists, or route it to index.php.            try_files $uri $uri/ /index.php;    }    location ~ \.php$ {            fastcgi_split_path_info ^(.+\.php)(/.+)$;            fastcgi_pass 127.0.0.1:9000;            fastcgi_index index.php;            include fastcgi_params;            fastcgi_param SCRIPT_FILENAME /path/to/my/php/code/$fastcgi_script_name;    }    location /redis {            proxy_set_header X-Real-IP $remote_addr;            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;            rewrite /(.*)/(.*)/(.*)$ /$2/$3 break; #ignore the /redis             proxy_redirect off;            proxy_pass http://webdisServerPool;    }}

On the front end side, here is an example of getting all the keys. All redis requests would go through /redis for example:

$.ajax({         url: "/redis/KEYS/*",         method: 'GET',         dataType: 'json',         success:function(data)        {            $each(data.KEYS,function(key,value){                            $('body').append(key+"=>"+value+" <br> ");            });        }});

OR

You could use:

http://wiki.nginx.org/HttpRedis and parse the response yourself.


I have found that the direct Redis http interfaces don't work very well with pub/sub or are difficult to set up (at time of writing).

Here is my "workaround" for pub/sub based on the predis examples.

http://bradleygoldsmith.tumblr.com/post/35601539836/quick-and-dirty-redis-subscribe-publish-notifications